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.19 (Berkeley) 03/02/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 alias(a, sendq); 256 } 257 258 /* 259 ** If the user is local and still being sent, verify that 260 ** the address is good. If it is, try to forward. 261 ** If the address is already good, we have a forwarding 262 ** loop. This can be broken by just sending directly to 263 ** the user (which is probably correct anyway). 264 */ 265 266 if (!bitset(QDONTSEND, a->q_flags) && m == LocalMailer) 267 { 268 struct stat stb; 269 extern bool writable(); 270 271 /* see if this is to a file */ 272 if (buf[0] == '/') 273 { 274 p = rindex(buf, '/'); 275 /* check if writable or creatable */ 276 if (a->q_alias == NULL && !QueueRun && !ForceMail) 277 { 278 a->q_flags |= QDONTSEND|QBADADDR; 279 usrerr("Cannot mail directly to files"); 280 } 281 else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) : 282 (*p = '\0', !safefile(buf, getruid(), S_IWRITE|S_IEXEC))) 283 { 284 a->q_flags |= QBADADDR; 285 giveresponse(EX_CANTCREAT, m, CurEnv); 286 } 287 } 288 else 289 { 290 register struct passwd *pw; 291 extern struct passwd *finduser(); 292 293 /* warning -- finduser may trash buf */ 294 pw = finduser(buf); 295 if (pw == NULL) 296 { 297 a->q_flags |= QBADADDR; 298 giveresponse(EX_NOUSER, m, CurEnv); 299 } 300 else 301 { 302 char nbuf[MAXNAME]; 303 304 if (strcmp(a->q_user, pw->pw_name) != 0) 305 { 306 a->q_user = newstr(pw->pw_name); 307 (void) strcpy(buf, pw->pw_name); 308 } 309 a->q_home = newstr(pw->pw_dir); 310 a->q_uid = pw->pw_uid; 311 a->q_gid = pw->pw_gid; 312 a->q_flags |= QGOODUID; 313 buildfname(pw->pw_gecos, pw->pw_name, nbuf); 314 if (nbuf[0] != '\0') 315 a->q_fullname = newstr(nbuf); 316 if (!quoted) 317 forward(a, sendq); 318 } 319 } 320 } 321 return (a); 322 } 323 /* 324 ** FINDUSER -- find the password entry for a user. 325 ** 326 ** This looks a lot like getpwnam, except that it may want to 327 ** do some fancier pattern matching in /etc/passwd. 328 ** 329 ** This routine contains most of the time of many sendmail runs. 330 ** It deserves to be optimized. 331 ** 332 ** Parameters: 333 ** name -- the name to match against. 334 ** 335 ** Returns: 336 ** A pointer to a pw struct. 337 ** NULL if name is unknown or ambiguous. 338 ** 339 ** Side Effects: 340 ** may modify name. 341 */ 342 343 struct passwd * 344 finduser(name) 345 char *name; 346 { 347 register struct passwd *pw; 348 register char *p; 349 extern struct passwd *getpwent(); 350 extern struct passwd *getpwnam(); 351 352 /* map upper => lower case */ 353 for (p = name; *p != '\0'; p++) 354 { 355 if (isascii(*p) && isupper(*p)) 356 *p = tolower(*p); 357 } 358 359 /* look up this login name using fast path */ 360 if ((pw = getpwnam(name)) != NULL) 361 return (pw); 362 363 /* search for a matching full name instead */ 364 for (p = name; *p != '\0'; p++) 365 { 366 if (*p == (SpaceSub & 0177) || *p == '_') 367 *p = ' '; 368 } 369 (void) setpwent(); 370 while ((pw = getpwent()) != NULL) 371 { 372 char buf[MAXNAME]; 373 374 buildfname(pw->pw_gecos, pw->pw_name, buf); 375 if (index(buf, ' ') != NULL && !strcasecmp(buf, name)) 376 { 377 message(Arpa_Info, "sending to login name %s", pw->pw_name); 378 return (pw); 379 } 380 } 381 return (NULL); 382 } 383 /* 384 ** WRITABLE -- predicate returning if the file is writable. 385 ** 386 ** This routine must duplicate the algorithm in sys/fio.c. 387 ** Unfortunately, we cannot use the access call since we 388 ** won't necessarily be the real uid when we try to 389 ** actually open the file. 390 ** 391 ** Notice that ANY file with ANY execute bit is automatically 392 ** not writable. This is also enforced by mailfile. 393 ** 394 ** Parameters: 395 ** s -- pointer to a stat struct for the file. 396 ** 397 ** Returns: 398 ** TRUE -- if we will be able to write this file. 399 ** FALSE -- if we cannot write this file. 400 ** 401 ** Side Effects: 402 ** none. 403 */ 404 405 bool 406 writable(s) 407 register struct stat *s; 408 { 409 int euid, egid; 410 int bits; 411 412 if (bitset(0111, s->st_mode)) 413 return (FALSE); 414 euid = getruid(); 415 egid = getrgid(); 416 if (geteuid() == 0) 417 { 418 if (bitset(S_ISUID, s->st_mode)) 419 euid = s->st_uid; 420 if (bitset(S_ISGID, s->st_mode)) 421 egid = s->st_gid; 422 } 423 424 if (euid == 0) 425 return (TRUE); 426 bits = S_IWRITE; 427 if (euid != s->st_uid) 428 { 429 bits >>= 3; 430 if (egid != s->st_gid) 431 bits >>= 3; 432 } 433 return ((s->st_mode & bits) != 0); 434 } 435 /* 436 ** INCLUDE -- handle :include: specification. 437 ** 438 ** Parameters: 439 ** fname -- filename to include. 440 ** msg -- message to print in verbose mode. 441 ** ctladdr -- address template to use to fill in these 442 ** addresses -- effective user/group id are 443 ** the important things. 444 ** sendq -- a pointer to the head of the send queue 445 ** to put these addresses in. 446 ** 447 ** Returns: 448 ** none. 449 ** 450 ** Side Effects: 451 ** reads the :include: file and sends to everyone 452 ** listed in that file. 453 */ 454 455 include(fname, msg, ctladdr, sendq) 456 char *fname; 457 char *msg; 458 ADDRESS *ctladdr; 459 ADDRESS **sendq; 460 { 461 char buf[MAXLINE]; 462 register FILE *fp; 463 char *oldto = CurEnv->e_to; 464 char *oldfilename = FileName; 465 int oldlinenumber = LineNumber; 466 467 fp = fopen(fname, "r"); 468 if (fp == NULL) 469 { 470 usrerr("Cannot open %s", fname); 471 return; 472 } 473 if (getctladdr(ctladdr) == NULL) 474 { 475 struct stat st; 476 477 if (fstat(fileno(fp), &st) < 0) 478 syserr("Cannot fstat %s!", fname); 479 ctladdr->q_uid = st.st_uid; 480 ctladdr->q_gid = st.st_gid; 481 ctladdr->q_flags |= QGOODUID; 482 } 483 484 /* read the file -- each line is a comma-separated list. */ 485 FileName = fname; 486 LineNumber = 0; 487 while (fgets(buf, sizeof buf, fp) != NULL) 488 { 489 register char *p = index(buf, '\n'); 490 491 LineNumber++; 492 if (p != NULL) 493 *p = '\0'; 494 if (buf[0] == '\0') 495 continue; 496 CurEnv->e_to = oldto; 497 message(Arpa_Info, "%s to %s", msg, buf); 498 AliasLevel++; 499 sendtolist(buf, ctladdr, sendq); 500 AliasLevel--; 501 } 502 503 (void) fclose(fp); 504 FileName = oldfilename; 505 LineNumber = oldlinenumber; 506 } 507 /* 508 ** SENDTOARGV -- send to an argument vector. 509 ** 510 ** Parameters: 511 ** argv -- argument vector to send to. 512 ** 513 ** Returns: 514 ** none. 515 ** 516 ** Side Effects: 517 ** puts all addresses on the argument vector onto the 518 ** send queue. 519 */ 520 521 sendtoargv(argv) 522 register char **argv; 523 { 524 register char *p; 525 526 while ((p = *argv++) != NULL) 527 { 528 if (argv[0] != NULL && argv[1] != NULL && !strcasecmp(argv[0], "at")) 529 { 530 char nbuf[MAXNAME]; 531 532 if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf) 533 usrerr("address overflow"); 534 else 535 { 536 (void) strcpy(nbuf, p); 537 (void) strcat(nbuf, "@"); 538 (void) strcat(nbuf, argv[1]); 539 p = newstr(nbuf); 540 argv += 2; 541 } 542 } 543 sendtolist(p, (ADDRESS *) NULL, &CurEnv->e_sendqueue); 544 } 545 } 546 /* 547 ** GETCTLADDR -- get controlling address from an address header. 548 ** 549 ** If none, get one corresponding to the effective userid. 550 ** 551 ** Parameters: 552 ** a -- the address to find the controller of. 553 ** 554 ** Returns: 555 ** the controlling address. 556 ** 557 ** Side Effects: 558 ** none. 559 */ 560 561 ADDRESS * 562 getctladdr(a) 563 register ADDRESS *a; 564 { 565 while (a != NULL && !bitset(QGOODUID, a->q_flags)) 566 a = a->q_alias; 567 return (a); 568 } 569