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 5.22 (Berkeley) 10/11/91"; 11 #endif /* not lint */ 12 13 # include <sys/types.h> 14 # include <sys/stat.h> 15 # include <pwd.h> 16 # include "sendmail.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 ** 33 ** Returns: 34 ** none 35 ** 36 ** Side Effects: 37 ** none. 38 */ 39 40 # define MAXRCRSN 10 41 42 sendtolist(list, ctladdr, sendq) 43 char *list; 44 ADDRESS *ctladdr; 45 ADDRESS **sendq; 46 { 47 register char *p; 48 register ADDRESS *al; /* list of addresses to send to */ 49 bool firstone; /* set on first address sent */ 50 bool selfref; /* set if this list includes ctladdr */ 51 char delimiter; /* the address delimiter */ 52 53 if (tTd(25, 1)) 54 { 55 printf("sendto: %s\n ctladdr=", list); 56 printaddr(ctladdr, FALSE); 57 } 58 59 /* heuristic to determine old versus new style addresses */ 60 if (ctladdr == NULL && 61 (index(list, ',') != NULL || index(list, ';') != NULL || 62 index(list, '<') != NULL || index(list, '(') != NULL)) 63 CurEnv->e_flags &= ~EF_OLDSTYLE; 64 delimiter = ' '; 65 if (!bitset(EF_OLDSTYLE, CurEnv->e_flags) || ctladdr != NULL) 66 delimiter = ','; 67 68 firstone = TRUE; 69 selfref = FALSE; 70 al = NULL; 71 72 for (p = list; *p != '\0'; ) 73 { 74 register ADDRESS *a; 75 extern char *DelimChar; /* defined in prescan */ 76 77 /* parse the address */ 78 while (isspace(*p) || *p == ',') 79 p++; 80 a = parseaddr(p, (ADDRESS *) NULL, 1, delimiter); 81 p = DelimChar; 82 if (a == NULL) 83 continue; 84 a->q_next = al; 85 a->q_alias = ctladdr; 86 87 /* see if this should be marked as a primary address */ 88 if (ctladdr == NULL || 89 (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags))) 90 a->q_flags |= QPRIMARY; 91 92 /* put on send queue or suppress self-reference */ 93 if (ctladdr != NULL && sameaddr(ctladdr, a)) 94 selfref = TRUE; 95 else 96 al = a; 97 firstone = FALSE; 98 } 99 100 /* if this alias doesn't include itself, delete ctladdr */ 101 if (!selfref && ctladdr != NULL) 102 ctladdr->q_flags |= QDONTSEND; 103 104 /* arrange to send to everyone on the local send list */ 105 while (al != NULL) 106 { 107 register ADDRESS *a = al; 108 extern ADDRESS *recipient(); 109 110 al = a->q_next; 111 setctladdr(a); 112 a = recipient(a, sendq); 113 114 /* arrange to inherit full name */ 115 if (a->q_fullname == NULL && ctladdr != NULL) 116 a->q_fullname = ctladdr->q_fullname; 117 } 118 119 CurEnv->e_to = NULL; 120 } 121 /* 122 ** RECIPIENT -- Designate a message recipient 123 ** 124 ** Saves the named person for future mailing. 125 ** 126 ** Parameters: 127 ** a -- the (preparsed) address header for the recipient. 128 ** sendq -- a pointer to the head of a queue to put the 129 ** recipient in. Duplicate supression is done 130 ** in this queue. 131 ** 132 ** Returns: 133 ** The actual address in the queue. This will be "a" if 134 ** the address is not a duplicate, else the original address. 135 ** 136 ** Side Effects: 137 ** none. 138 */ 139 140 extern ADDRESS *getctladdr(); 141 142 ADDRESS * 143 recipient(a, sendq) 144 register ADDRESS *a; 145 register ADDRESS **sendq; 146 { 147 register ADDRESS *q; 148 ADDRESS **pq; 149 register struct mailer *m; 150 register char *p; 151 bool quoted = FALSE; /* set if the addr has a quote bit */ 152 char buf[MAXNAME]; /* unquoted image of the user name */ 153 extern bool safefile(); 154 155 CurEnv->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("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 /* map user & host to lower case if requested on non-aliases */ 179 if (a->q_alias == NULL) 180 loweraddr(a); 181 182 /* get unquoted user for file, program or user.name check */ 183 (void) strcpy(buf, a->q_user); 184 for (p = buf; *p != '\0' && !quoted; p++) 185 { 186 if (!isascii(*p) && (*p & 0377) != (SpaceSub & 0377)) 187 quoted = TRUE; 188 } 189 stripquotes(buf, TRUE); 190 191 /* do sickly crude mapping for program mailing, etc. */ 192 if (m == LocalMailer && buf[0] == '|') 193 { 194 a->q_mailer = m = ProgMailer; 195 a->q_user++; 196 if (a->q_alias == NULL && !QueueRun && !ForceMail) 197 { 198 a->q_flags |= QDONTSEND|QBADADDR; 199 usrerr("Cannot mail directly to programs"); 200 } 201 } 202 203 /* 204 ** Look up this person in the recipient list. 205 ** If they are there already, return, otherwise continue. 206 ** If the list is empty, just add it. Notice the cute 207 ** hack to make from addresses suppress things correctly: 208 ** the QDONTSEND bit will be set in the send list. 209 ** [Please note: the emphasis is on "hack."] 210 */ 211 212 for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next) 213 { 214 if (!ForceMail && sameaddr(q, a)) 215 { 216 if (tTd(26, 1)) 217 { 218 printf("%s in sendq: ", a->q_paddr); 219 printaddr(q, FALSE); 220 } 221 if (!bitset(QDONTSEND, a->q_flags)) 222 message(Arpa_Info, "duplicate suppressed"); 223 if (!bitset(QPRIMARY, q->q_flags)) 224 q->q_flags |= a->q_flags; 225 return (q); 226 } 227 } 228 229 /* add address on list */ 230 *pq = a; 231 a->q_next = NULL; 232 CurEnv->e_nrcpts++; 233 234 /* 235 ** Alias the name and handle :include: specs. 236 */ 237 238 if (m == LocalMailer && !bitset(QDONTSEND, a->q_flags)) 239 { 240 if (strncmp(a->q_user, ":include:", 9) == 0) 241 { 242 a->q_flags |= QDONTSEND; 243 if (a->q_alias == NULL && !QueueRun && !ForceMail) 244 { 245 a->q_flags |= QBADADDR; 246 usrerr("Cannot mail directly to :include:s"); 247 } 248 else 249 { 250 message(Arpa_Info, "including file %s", &a->q_user[9]); 251 include(&a->q_user[9], " sending", a, sendq); 252 } 253 } 254 else 255 { 256 /* try aliasing */ 257 alias(a, sendq); 258 259 # ifdef USERDB 260 /* if not aliased, look it up in the user database */ 261 if (!bitset(QDONTSEND|QNOTREMOTE, a->q_flags)) 262 udbexpand(a, sendq); 263 # endif 264 } 265 } 266 267 /* 268 ** If the user is local and still being sent, verify that 269 ** the address is good. If it is, try to forward. 270 ** If the address is already good, we have a forwarding 271 ** loop. This can be broken by just sending directly to 272 ** the user (which is probably correct anyway). 273 */ 274 275 if (bitset(QDONTSEND, a->q_flags) || m != LocalMailer) 276 return (a); 277 278 /* see if this is to a file */ 279 if (buf[0] == '/') 280 { 281 struct stat stb; 282 extern bool writable(); 283 284 p = rindex(buf, '/'); 285 /* check if writable or creatable */ 286 if (a->q_alias == NULL && !QueueRun && !ForceMail) 287 { 288 a->q_flags |= QDONTSEND|QBADADDR; 289 usrerr("Cannot mail directly to files"); 290 } 291 else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) : 292 (*p = '\0', !safefile(buf, getruid(), S_IWRITE|S_IEXEC))) 293 { 294 a->q_flags |= QBADADDR; 295 giveresponse(EX_CANTCREAT, m, CurEnv); 296 } 297 return (a); 298 } 299 300 /* 301 ** If we have a level two config file, then pass the name through 302 ** Ruleset 5 before sending it off. Ruleset 5 has the right 303 ** to send rewrite it to another mailer. This gives us a hook 304 ** after local aliasing has been done. 305 */ 306 307 if (tTd(29, 5)) 308 { 309 printf("recipient: testing local? cl=%d, rr5=%x\n\t", 310 ConfigLevel, RewriteRules[5]); 311 printaddr(a, FALSE); 312 } 313 if (!bitset(QNOTREMOTE, a->q_flags) && ConfigLevel >= 2 && 314 RewriteRules[5] != NULL) 315 { 316 maplocaluser(a, sendq); 317 } 318 319 /* 320 ** If it didn't get rewritten to another mailer, go ahead 321 ** and deliver it. 322 */ 323 324 if (!bitset(QDONTSEND, a->q_flags)) 325 { 326 register struct passwd *pw; 327 extern struct passwd *finduser(); 328 329 /* warning -- finduser may trash buf */ 330 pw = finduser(buf); 331 if (pw == NULL) 332 { 333 a->q_flags |= QBADADDR; 334 giveresponse(EX_NOUSER, m, CurEnv); 335 } 336 else 337 { 338 char nbuf[MAXNAME]; 339 340 if (strcmp(a->q_user, pw->pw_name) != 0) 341 { 342 a->q_user = newstr(pw->pw_name); 343 (void) strcpy(buf, pw->pw_name); 344 } 345 a->q_home = newstr(pw->pw_dir); 346 a->q_uid = pw->pw_uid; 347 a->q_gid = pw->pw_gid; 348 a->q_flags |= QGOODUID; 349 buildfname(pw->pw_gecos, pw->pw_name, nbuf); 350 if (nbuf[0] != '\0') 351 a->q_fullname = newstr(nbuf); 352 if (!quoted) 353 forward(a, sendq); 354 } 355 } 356 return (a); 357 } 358 /* 359 ** FINDUSER -- find the password entry for a user. 360 ** 361 ** This looks a lot like getpwnam, except that it may want to 362 ** do some fancier pattern matching in /etc/passwd. 363 ** 364 ** This routine contains most of the time of many sendmail runs. 365 ** It deserves to be optimized. 366 ** 367 ** Parameters: 368 ** name -- the name to match against. 369 ** 370 ** Returns: 371 ** A pointer to a pw struct. 372 ** NULL if name is unknown or ambiguous. 373 ** 374 ** Side Effects: 375 ** may modify name. 376 */ 377 378 struct passwd * 379 finduser(name) 380 char *name; 381 { 382 register struct passwd *pw; 383 register char *p; 384 extern struct passwd *getpwent(); 385 extern struct passwd *getpwnam(); 386 387 /* map upper => lower case */ 388 for (p = name; *p != '\0'; p++) 389 { 390 if (isascii(*p) && isupper(*p)) 391 *p = tolower(*p); 392 } 393 394 /* look up this login name using fast path */ 395 if ((pw = getpwnam(name)) != NULL) 396 return (pw); 397 398 /* search for a matching full name instead */ 399 for (p = name; *p != '\0'; p++) 400 { 401 if (*p == (SpaceSub & 0177) || *p == '_') 402 *p = ' '; 403 } 404 (void) setpwent(); 405 while ((pw = getpwent()) != NULL) 406 { 407 char buf[MAXNAME]; 408 409 buildfname(pw->pw_gecos, pw->pw_name, buf); 410 if (index(buf, ' ') != NULL && !strcasecmp(buf, name)) 411 { 412 message(Arpa_Info, "sending to login name %s", pw->pw_name); 413 return (pw); 414 } 415 } 416 return (NULL); 417 } 418 /* 419 ** WRITABLE -- predicate returning if the file is writable. 420 ** 421 ** This routine must duplicate the algorithm in sys/fio.c. 422 ** Unfortunately, we cannot use the access call since we 423 ** won't necessarily be the real uid when we try to 424 ** actually open the file. 425 ** 426 ** Notice that ANY file with ANY execute bit is automatically 427 ** not writable. This is also enforced by mailfile. 428 ** 429 ** Parameters: 430 ** s -- pointer to a stat struct for the file. 431 ** 432 ** Returns: 433 ** TRUE -- if we will be able to write this file. 434 ** FALSE -- if we cannot write this file. 435 ** 436 ** Side Effects: 437 ** none. 438 */ 439 440 bool 441 writable(s) 442 register struct stat *s; 443 { 444 int euid, egid; 445 int bits; 446 447 if (bitset(0111, s->st_mode)) 448 return (FALSE); 449 euid = getruid(); 450 egid = getrgid(); 451 if (geteuid() == 0) 452 { 453 if (bitset(S_ISUID, s->st_mode)) 454 euid = s->st_uid; 455 if (bitset(S_ISGID, s->st_mode)) 456 egid = s->st_gid; 457 } 458 459 if (euid == 0) 460 return (TRUE); 461 bits = S_IWRITE; 462 if (euid != s->st_uid) 463 { 464 bits >>= 3; 465 if (egid != s->st_gid) 466 bits >>= 3; 467 } 468 return ((s->st_mode & bits) != 0); 469 } 470 /* 471 ** INCLUDE -- handle :include: specification. 472 ** 473 ** Parameters: 474 ** fname -- filename to include. 475 ** msg -- message to print in verbose mode. 476 ** ctladdr -- address template to use to fill in these 477 ** addresses -- effective user/group id are 478 ** the important things. 479 ** sendq -- a pointer to the head of the send queue 480 ** to put these addresses in. 481 ** 482 ** Returns: 483 ** none. 484 ** 485 ** Side Effects: 486 ** reads the :include: file and sends to everyone 487 ** listed in that file. 488 */ 489 490 include(fname, msg, ctladdr, sendq) 491 char *fname; 492 char *msg; 493 ADDRESS *ctladdr; 494 ADDRESS **sendq; 495 { 496 char buf[MAXLINE]; 497 register FILE *fp; 498 char *oldto = CurEnv->e_to; 499 char *oldfilename = FileName; 500 int oldlinenumber = LineNumber; 501 502 fp = fopen(fname, "r"); 503 if (fp == NULL) 504 { 505 usrerr("Cannot open %s", fname); 506 return; 507 } 508 if (getctladdr(ctladdr) == NULL) 509 { 510 struct stat st; 511 512 if (fstat(fileno(fp), &st) < 0) 513 syserr("Cannot fstat %s!", fname); 514 ctladdr->q_uid = st.st_uid; 515 ctladdr->q_gid = st.st_gid; 516 ctladdr->q_flags |= QGOODUID; 517 } 518 519 /* read the file -- each line is a comma-separated list. */ 520 FileName = fname; 521 LineNumber = 0; 522 while (fgets(buf, sizeof buf, fp) != NULL) 523 { 524 register char *p = index(buf, '\n'); 525 526 LineNumber++; 527 if (p != NULL) 528 *p = '\0'; 529 if (buf[0] == '\0') 530 continue; 531 CurEnv->e_to = oldto; 532 message(Arpa_Info, "%s to %s", msg, buf); 533 AliasLevel++; 534 sendtolist(buf, ctladdr, sendq); 535 AliasLevel--; 536 } 537 538 (void) fclose(fp); 539 FileName = oldfilename; 540 LineNumber = oldlinenumber; 541 } 542 /* 543 ** SENDTOARGV -- send to an argument vector. 544 ** 545 ** Parameters: 546 ** argv -- argument vector to send to. 547 ** 548 ** Returns: 549 ** none. 550 ** 551 ** Side Effects: 552 ** puts all addresses on the argument vector onto the 553 ** send queue. 554 */ 555 556 sendtoargv(argv) 557 register char **argv; 558 { 559 register char *p; 560 561 while ((p = *argv++) != NULL) 562 { 563 if (argv[0] != NULL && argv[1] != NULL && !strcasecmp(argv[0], "at")) 564 { 565 char nbuf[MAXNAME]; 566 567 if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf) 568 usrerr("address overflow"); 569 else 570 { 571 (void) strcpy(nbuf, p); 572 (void) strcat(nbuf, "@"); 573 (void) strcat(nbuf, argv[1]); 574 p = newstr(nbuf); 575 argv += 2; 576 } 577 } 578 sendtolist(p, (ADDRESS *) NULL, &CurEnv->e_sendqueue); 579 } 580 } 581 /* 582 ** GETCTLADDR -- get controlling address from an address header. 583 ** 584 ** If none, get one corresponding to the effective userid. 585 ** 586 ** Parameters: 587 ** a -- the address to find the controller of. 588 ** 589 ** Returns: 590 ** the controlling address. 591 ** 592 ** Side Effects: 593 ** none. 594 */ 595 596 ADDRESS * 597 getctladdr(a) 598 register ADDRESS *a; 599 { 600 while (a != NULL && !bitset(QGOODUID, a->q_flags)) 601 a = a->q_alias; 602 return (a); 603 } 604