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