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