1 /* 2 * Copyright (c) 1983 Eric P. Allman 3 * Copyright (c) 1988 Regents of the University of California. 4 * All rights reserved. 5 * 6 * %sccs.include.redist.c% 7 */ 8 9 #ifndef lint 10 static char sccsid[] = "@(#)recipient.c 6.27 (Berkeley) 03/16/93"; 11 #endif /* not lint */ 12 13 # include "sendmail.h" 14 # include <sys/stat.h> 15 # include <fcntl.h> 16 # include <pwd.h> 17 18 /* 19 ** SENDTOLIST -- Designate a send list. 20 ** 21 ** The parameter is a comma-separated list of people to send to. 22 ** This routine arranges to send to all of them. 23 ** 24 ** Parameters: 25 ** list -- the send list. 26 ** ctladdr -- the address template for the person to 27 ** send to -- effective uid/gid are important. 28 ** This is typically the alias that caused this 29 ** expansion. 30 ** sendq -- a pointer to the head of a queue to put 31 ** these people into. 32 ** e -- the envelope in which to add these recipients. 33 ** 34 ** Returns: 35 ** The number of addresses actually on the list. 36 ** 37 ** Side Effects: 38 ** none. 39 */ 40 41 # define MAXRCRSN 10 42 43 sendtolist(list, ctladdr, sendq, e) 44 char *list; 45 ADDRESS *ctladdr; 46 ADDRESS **sendq; 47 register ENVELOPE *e; 48 { 49 register char *p; 50 register ADDRESS *al; /* list of addresses to send to */ 51 bool firstone; /* set on first address sent */ 52 char delimiter; /* the address delimiter */ 53 int naddrs; 54 55 if (tTd(25, 1)) 56 { 57 printf("sendto: %s\n ctladdr=", list); 58 printaddr(ctladdr, FALSE); 59 } 60 61 /* heuristic to determine old versus new style addresses */ 62 if (ctladdr == NULL && 63 (strchr(list, ',') != NULL || strchr(list, ';') != NULL || 64 strchr(list, '<') != NULL || strchr(list, '(') != NULL)) 65 e->e_flags &= ~EF_OLDSTYLE; 66 delimiter = ' '; 67 if (!bitset(EF_OLDSTYLE, e->e_flags) || ctladdr != NULL) 68 delimiter = ','; 69 70 firstone = TRUE; 71 al = NULL; 72 naddrs = 0; 73 74 for (p = list; *p != '\0'; ) 75 { 76 auto char *delimptr; 77 register ADDRESS *a; 78 79 /* parse the address */ 80 while ((isascii(*p) && isspace(*p)) || *p == ',') 81 p++; 82 a = parseaddr(p, (ADDRESS *) NULL, 1, delimiter, &delimptr, e); 83 p = delimptr; 84 if (a == NULL) 85 continue; 86 a->q_next = al; 87 a->q_alias = ctladdr; 88 89 /* see if this should be marked as a primary address */ 90 if (ctladdr == NULL || 91 (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags))) 92 a->q_flags |= QPRIMARY; 93 94 if (ctladdr != NULL && sameaddr(ctladdr, a)) 95 ctladdr->q_flags |= QSELFREF; 96 al = a; 97 firstone = FALSE; 98 } 99 100 /* arrange to send to everyone on the local send list */ 101 while (al != NULL) 102 { 103 register ADDRESS *a = al; 104 extern ADDRESS *recipient(); 105 106 al = a->q_next; 107 a = recipient(a, sendq, e); 108 109 /* arrange to inherit full name */ 110 if (a->q_fullname == NULL && ctladdr != NULL) 111 a->q_fullname = ctladdr->q_fullname; 112 naddrs++; 113 } 114 115 e->e_to = NULL; 116 return (naddrs); 117 } 118 /* 119 ** RECIPIENT -- Designate a message recipient 120 ** 121 ** Saves the named person for future mailing. 122 ** 123 ** Parameters: 124 ** a -- the (preparsed) address header for the recipient. 125 ** sendq -- a pointer to the head of a queue to put the 126 ** recipient in. Duplicate supression is done 127 ** in this queue. 128 ** e -- the current envelope. 129 ** 130 ** Returns: 131 ** The actual address in the queue. This will be "a" if 132 ** the address is not a duplicate, else the original address. 133 ** 134 ** Side Effects: 135 ** none. 136 */ 137 138 extern ADDRESS *getctladdr(); 139 140 ADDRESS * 141 recipient(a, sendq, e) 142 register ADDRESS *a; 143 register ADDRESS **sendq; 144 register ENVELOPE *e; 145 { 146 register ADDRESS *q; 147 ADDRESS **pq; 148 register struct mailer *m; 149 register char *p; 150 bool quoted = FALSE; /* set if the addr has a quote bit */ 151 int findusercount = 0; 152 char buf[MAXNAME]; /* unquoted image of the user name */ 153 extern int safefile(); 154 155 e->e_to = a->q_paddr; 156 m = a->q_mailer; 157 errno = 0; 158 if (tTd(26, 1)) 159 { 160 printf("\nrecipient: "); 161 printaddr(a, FALSE); 162 } 163 164 /* break aliasing loops */ 165 if (AliasLevel > MAXRCRSN) 166 { 167 usrerr("554 aliasing/forwarding loop broken"); 168 return (a); 169 } 170 171 /* 172 ** Finish setting up address structure. 173 */ 174 175 /* set the queue timeout */ 176 a->q_timeout = TimeOut; 177 178 /* get unquoted user for file, program or user.name check */ 179 (void) strcpy(buf, a->q_user); 180 for (p = buf; *p != '\0' && !quoted; p++) 181 { 182 if (*p == '\\') 183 quoted = TRUE; 184 } 185 stripquotes(buf); 186 187 /* check for direct mailing to restricted mailers */ 188 if (a->q_alias == NULL && m == ProgMailer) 189 { 190 a->q_flags |= QBADADDR; 191 usrerr("550 Cannot mail directly to programs", m->m_name); 192 } 193 194 /* 195 ** Look up this person in the recipient list. 196 ** If they are there already, return, otherwise continue. 197 ** If the list is empty, just add it. Notice the cute 198 ** hack to make from addresses suppress things correctly: 199 ** the QDONTSEND bit will be set in the send list. 200 ** [Please note: the emphasis is on "hack."] 201 */ 202 203 for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next) 204 { 205 if (sameaddr(q, a)) 206 { 207 if (tTd(26, 1)) 208 { 209 printf("%s in sendq: ", a->q_paddr); 210 printaddr(q, FALSE); 211 } 212 if (!bitset(QPRIMARY, q->q_flags)) 213 { 214 if (!bitset(QDONTSEND, a->q_flags)) 215 message("duplicate suppressed"); 216 q->q_flags |= a->q_flags; 217 } 218 return (q); 219 } 220 } 221 222 /* add address on list */ 223 *pq = a; 224 a->q_next = NULL; 225 226 /* 227 ** Alias the name and handle special mailer types. 228 */ 229 230 trylocaluser: 231 if (tTd(29, 7)) 232 printf("at trylocaluser %s\n", a->q_user); 233 234 if (bitset(QDONTSEND|QBADADDR|QVERIFIED, a->q_flags)) 235 return (a); 236 237 if (m == InclMailer) 238 { 239 a->q_flags |= QDONTSEND; 240 if (a->q_alias == NULL) 241 { 242 a->q_flags |= QBADADDR; 243 usrerr("550 Cannot mail directly to :include:s"); 244 } 245 else 246 { 247 int err; 248 249 message("including file %s", a->q_user); 250 err = include(a->q_user, FALSE, a, sendq, e); 251 if (transienterror(err)) 252 a->q_flags |= QQUEUEUP|QDONTSEND; 253 } 254 } 255 else if (m == FileMailer) 256 { 257 struct stat stb; 258 extern bool writable(); 259 260 p = strrchr(buf, '/'); 261 /* check if writable or creatable */ 262 if (a->q_alias == NULL && !QueueRun) 263 { 264 a->q_flags |= QBADADDR; 265 usrerr("550 Cannot mail directly to files"); 266 } 267 else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) : 268 (*p = '\0', safefile(buf, getruid(), S_IWRITE|S_IEXEC) != 0)) 269 { 270 a->q_flags |= QBADADDR; 271 giveresponse(EX_CANTCREAT, m, NULL, e); 272 } 273 } 274 275 if (m != LocalMailer) 276 { 277 if (!bitset(QDONTSEND, a->q_flags)) 278 e->e_nrcpts++; 279 return (a); 280 } 281 282 /* try aliasing */ 283 alias(a, sendq, e); 284 285 # ifdef USERDB 286 /* if not aliased, look it up in the user database */ 287 if (!bitset(QDONTSEND|QNOTREMOTE, a->q_flags)) 288 { 289 extern int udbexpand(); 290 291 if (udbexpand(a, sendq, e) == EX_TEMPFAIL) 292 { 293 a->q_flags |= QQUEUEUP|QDONTSEND; 294 if (e->e_message == NULL) 295 e->e_message = newstr("Deferred: user database error"); 296 # ifdef LOG 297 if (LogLevel > 8) 298 syslog(LOG_INFO, "%s: deferred: udbexpand", 299 e->e_id); 300 # endif 301 message("queued (user database error)"); 302 e->e_nrcpts++; 303 return (a); 304 } 305 } 306 # endif 307 308 /* if it was an alias or a UDB expansion, just return now */ 309 if (bitset(QDONTSEND|QQUEUEUP|QVERIFIED, a->q_flags)) 310 return (a); 311 312 /* 313 ** If we have a level two config file, then pass the name through 314 ** Ruleset 5 before sending it off. Ruleset 5 has the right 315 ** to send rewrite it to another mailer. This gives us a hook 316 ** after local aliasing has been done. 317 */ 318 319 if (tTd(29, 5)) 320 { 321 printf("recipient: testing local? cl=%d, rr5=%x\n\t", 322 ConfigLevel, RewriteRules[5]); 323 printaddr(a, FALSE); 324 } 325 if (!bitset(QNOTREMOTE, a->q_flags) && ConfigLevel >= 2 && 326 RewriteRules[5] != NULL) 327 { 328 maplocaluser(a, sendq, e); 329 } 330 331 /* 332 ** If it didn't get rewritten to another mailer, go ahead 333 ** and deliver it. 334 */ 335 336 if (!bitset(QDONTSEND|QQUEUEUP, a->q_flags)) 337 { 338 auto bool fuzzy; 339 register struct passwd *pw; 340 extern struct passwd *finduser(); 341 342 /* warning -- finduser may trash buf */ 343 pw = finduser(buf, &fuzzy); 344 if (pw == NULL) 345 { 346 a->q_flags |= QBADADDR; 347 giveresponse(EX_NOUSER, m, NULL, e); 348 } 349 else 350 { 351 char nbuf[MAXNAME]; 352 353 if (fuzzy) 354 { 355 /* name was a fuzzy match */ 356 a->q_user = newstr(pw->pw_name); 357 if (findusercount++ > 3) 358 { 359 a->q_flags |= QBADADDR; 360 usrerr("554 aliasing/forwarding loop for %s broken", 361 pw->pw_name); 362 return (a); 363 } 364 365 /* see if it aliases */ 366 (void) strcpy(buf, pw->pw_name); 367 goto trylocaluser; 368 } 369 a->q_home = newstr(pw->pw_dir); 370 a->q_uid = pw->pw_uid; 371 a->q_gid = pw->pw_gid; 372 a->q_flags |= QGOODUID; 373 buildfname(pw->pw_gecos, pw->pw_name, nbuf); 374 if (nbuf[0] != '\0') 375 a->q_fullname = newstr(nbuf); 376 if (!quoted) 377 forward(a, sendq, e); 378 } 379 } 380 if (!bitset(QDONTSEND, a->q_flags)) 381 e->e_nrcpts++; 382 return (a); 383 } 384 /* 385 ** FINDUSER -- find the password entry for a user. 386 ** 387 ** This looks a lot like getpwnam, except that it may want to 388 ** do some fancier pattern matching in /etc/passwd. 389 ** 390 ** This routine contains most of the time of many sendmail runs. 391 ** It deserves to be optimized. 392 ** 393 ** Parameters: 394 ** name -- the name to match against. 395 ** fuzzyp -- an outarg that is set to TRUE if this entry 396 ** was found using the fuzzy matching algorithm; 397 ** set to FALSE otherwise. 398 ** 399 ** Returns: 400 ** A pointer to a pw struct. 401 ** NULL if name is unknown or ambiguous. 402 ** 403 ** Side Effects: 404 ** may modify name. 405 */ 406 407 struct passwd * 408 finduser(name, fuzzyp) 409 char *name; 410 bool *fuzzyp; 411 { 412 register struct passwd *pw; 413 register char *p; 414 extern struct passwd *getpwent(); 415 extern struct passwd *getpwnam(); 416 417 if (tTd(29, 4)) 418 printf("finduser(%s): ", name); 419 420 /* map upper => lower case */ 421 for (p = name; *p != '\0'; p++) 422 { 423 if (isascii(*p) && isupper(*p)) 424 *p = tolower(*p); 425 } 426 *fuzzyp = FALSE; 427 428 /* look up this login name using fast path */ 429 if ((pw = getpwnam(name)) != NULL) 430 { 431 if (tTd(29, 4)) 432 printf("found (non-fuzzy)\n"); 433 return (pw); 434 } 435 436 #ifdef MATCHGECOS 437 /* see if fuzzy matching allowed */ 438 if (!MatchGecos) 439 { 440 if (tTd(29, 4)) 441 printf("not found (fuzzy disabled)\n"); 442 return NULL; 443 } 444 445 /* search for a matching full name instead */ 446 for (p = name; *p != '\0'; p++) 447 { 448 if (*p == (SpaceSub & 0177) || *p == '_') 449 *p = ' '; 450 } 451 (void) setpwent(); 452 while ((pw = getpwent()) != NULL) 453 { 454 char buf[MAXNAME]; 455 456 buildfname(pw->pw_gecos, pw->pw_name, buf); 457 if (strchr(buf, ' ') != NULL && !strcasecmp(buf, name)) 458 { 459 if (tTd(29, 4)) 460 printf("fuzzy matches %s\n", pw->pw_name); 461 message("sending to login name %s", pw->pw_name); 462 *fuzzyp = TRUE; 463 return (pw); 464 } 465 } 466 #endif 467 if (tTd(29, 4)) 468 printf("no fuzzy match found\n"); 469 return (NULL); 470 } 471 /* 472 ** WRITABLE -- predicate returning if the file is writable. 473 ** 474 ** This routine must duplicate the algorithm in sys/fio.c. 475 ** Unfortunately, we cannot use the access call since we 476 ** won't necessarily be the real uid when we try to 477 ** actually open the file. 478 ** 479 ** Notice that ANY file with ANY execute bit is automatically 480 ** not writable. This is also enforced by mailfile. 481 ** 482 ** Parameters: 483 ** s -- pointer to a stat struct for the file. 484 ** 485 ** Returns: 486 ** TRUE -- if we will be able to write this file. 487 ** FALSE -- if we cannot write this file. 488 ** 489 ** Side Effects: 490 ** none. 491 */ 492 493 bool 494 writable(s) 495 register struct stat *s; 496 { 497 uid_t euid; 498 gid_t egid; 499 int bits; 500 501 if (bitset(0111, s->st_mode)) 502 return (FALSE); 503 euid = getruid(); 504 egid = getrgid(); 505 if (geteuid() == 0) 506 { 507 if (bitset(S_ISUID, s->st_mode)) 508 euid = s->st_uid; 509 if (bitset(S_ISGID, s->st_mode)) 510 egid = s->st_gid; 511 } 512 513 if (euid == 0) 514 return (TRUE); 515 bits = S_IWRITE; 516 if (euid != s->st_uid) 517 { 518 bits >>= 3; 519 if (egid != s->st_gid) 520 bits >>= 3; 521 } 522 return ((s->st_mode & bits) != 0); 523 } 524 /* 525 ** INCLUDE -- handle :include: specification. 526 ** 527 ** Parameters: 528 ** fname -- filename to include. 529 ** forwarding -- if TRUE, we are reading a .forward file. 530 ** if FALSE, it's a :include: file. 531 ** ctladdr -- address template to use to fill in these 532 ** addresses -- effective user/group id are 533 ** the important things. 534 ** sendq -- a pointer to the head of the send queue 535 ** to put these addresses in. 536 ** 537 ** Returns: 538 ** open error status 539 ** 540 ** Side Effects: 541 ** reads the :include: file and sends to everyone 542 ** listed in that file. 543 */ 544 545 static jmp_buf CtxIncludeTimeout; 546 547 int 548 include(fname, forwarding, ctladdr, sendq, e) 549 char *fname; 550 bool forwarding; 551 ADDRESS *ctladdr; 552 ADDRESS **sendq; 553 ENVELOPE *e; 554 { 555 register FILE *fp; 556 char *oldto = e->e_to; 557 char *oldfilename = FileName; 558 int oldlinenumber = LineNumber; 559 register EVENT *ev = NULL; 560 int nincludes; 561 int ret; 562 char buf[MAXLINE]; 563 static int includetimeout(); 564 565 if (tTd(27, 2)) 566 printf("include(%s)\n", fname); 567 568 /* 569 ** If home directory is remote mounted but server is down, 570 ** this can hang or give errors; use a timeout to avoid this 571 */ 572 573 if (setjmp(CtxIncludeTimeout) != 0) 574 { 575 ctladdr->q_flags |= QQUEUEUP|QDONTSEND; 576 errno = 0; 577 usrerr("451 open timeout on %s", fname); 578 return ETIMEDOUT; 579 } 580 ev = setevent((time_t) 60, includetimeout, 0); 581 582 /* if forwarding, the input file must be marked safe */ 583 if (forwarding && (ret = safefile(fname, ctladdr->q_uid, S_IREAD)) != 0) 584 { 585 /* don't use this .forward file */ 586 clrevent(ev); 587 if (tTd(27, 4)) 588 printf("include: not safe (uid=%d): %s\n", 589 ctladdr->q_uid, errstring(ret)); 590 return ret; 591 } 592 593 fp = fopen(fname, "r"); 594 if (fp == NULL) 595 { 596 int ret = errno; 597 598 clrevent(ev); 599 usrerr("550 Cannot open %s: %s", fname, errstring(ret)); 600 return ret; 601 } 602 603 if (getctladdr(ctladdr) == NULL) 604 { 605 struct stat st; 606 607 if (fstat(fileno(fp), &st) < 0) 608 { 609 int ret = errno; 610 611 clrevent(ev); 612 syserr("Cannot fstat %s!", fname); 613 return ret; 614 } 615 ctladdr->q_uid = st.st_uid; 616 ctladdr->q_gid = st.st_gid; 617 ctladdr->q_flags |= QGOODUID; 618 } 619 620 clrevent(ev); 621 622 if (bitset(EF_VRFYONLY, e->e_flags)) 623 { 624 /* don't do any more now */ 625 xfclose(fp, "include", fname); 626 return 0; 627 } 628 629 /* read the file -- each line is a comma-separated list. */ 630 FileName = fname; 631 LineNumber = 0; 632 ctladdr->q_flags &= ~QSELFREF; 633 nincludes = 0; 634 while (fgets(buf, sizeof buf, fp) != NULL) 635 { 636 register char *p = strchr(buf, '\n'); 637 638 LineNumber++; 639 if (p != NULL) 640 *p = '\0'; 641 if (buf[0] == '#' || buf[0] == '\0') 642 continue; 643 e->e_to = NULL; 644 message("%s to %s", 645 forwarding ? "forwarding" : "sending", buf); 646 #ifdef LOG 647 if (forwarding && LogLevel > 9) 648 syslog(LOG_INFO, "%s: forward %s => %s", 649 e->e_id, oldto, buf); 650 #endif 651 652 AliasLevel++; 653 nincludes += sendtolist(buf, ctladdr, sendq, e); 654 AliasLevel--; 655 } 656 if (nincludes > 0 && !bitset(QSELFREF, ctladdr->q_flags)) 657 { 658 if (tTd(27, 5)) 659 { 660 printf("include: QDONTSEND "); 661 printaddr(ctladdr, FALSE); 662 } 663 ctladdr->q_flags |= QDONTSEND; 664 } 665 666 (void) xfclose(fp, "include", fname); 667 FileName = oldfilename; 668 LineNumber = oldlinenumber; 669 return 0; 670 } 671 672 static 673 includetimeout() 674 { 675 longjmp(CtxIncludeTimeout, 1); 676 } 677 /* 678 ** SENDTOARGV -- send to an argument vector. 679 ** 680 ** Parameters: 681 ** argv -- argument vector to send to. 682 ** e -- the current envelope. 683 ** 684 ** Returns: 685 ** none. 686 ** 687 ** Side Effects: 688 ** puts all addresses on the argument vector onto the 689 ** send queue. 690 */ 691 692 sendtoargv(argv, e) 693 register char **argv; 694 register ENVELOPE *e; 695 { 696 register char *p; 697 698 while ((p = *argv++) != NULL) 699 { 700 (void) sendtolist(p, (ADDRESS *) NULL, &e->e_sendqueue, e); 701 } 702 } 703 /* 704 ** GETCTLADDR -- get controlling address from an address header. 705 ** 706 ** If none, get one corresponding to the effective userid. 707 ** 708 ** Parameters: 709 ** a -- the address to find the controller of. 710 ** 711 ** Returns: 712 ** the controlling address. 713 ** 714 ** Side Effects: 715 ** none. 716 */ 717 718 ADDRESS * 719 getctladdr(a) 720 register ADDRESS *a; 721 { 722 while (a != NULL && !bitset(QGOODUID, a->q_flags)) 723 a = a->q_alias; 724 return (a); 725 } 726