1 /* $NetBSD: names.c,v 1.17 2003/08/07 11:14:40 agc Exp $ */ 2 3 /* 4 * Copyright (c) 1980, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #ifndef lint 34 #if 0 35 static char sccsid[] = "@(#)names.c 8.1 (Berkeley) 6/6/93"; 36 #else 37 __RCSID("$NetBSD: names.c,v 1.17 2003/08/07 11:14:40 agc Exp $"); 38 #endif 39 #endif /* not lint */ 40 41 /* 42 * Mail -- a mail program 43 * 44 * Handle name lists. 45 */ 46 47 #include "rcv.h" 48 #include "extern.h" 49 50 extern char *tmpdir; 51 52 /* 53 * Allocate a single element of a name list, 54 * initialize its name field to the passed 55 * name and return it. 56 */ 57 struct name * 58 nalloc(char str[], int ntype) 59 { 60 struct name *np; 61 62 np = (struct name *) salloc(sizeof *np); 63 np->n_flink = NULL; 64 np->n_blink = NULL; 65 np->n_type = ntype; 66 np->n_name = savestr(str); 67 return(np); 68 } 69 70 /* 71 * Find the tail of a list and return it. 72 */ 73 struct name * 74 tailof(struct name *name) 75 { 76 struct name *np; 77 78 np = name; 79 if (np == NULL) 80 return(NULL); 81 while (np->n_flink != NULL) 82 np = np->n_flink; 83 return(np); 84 } 85 86 /* 87 * Extract a list of names from a line, 88 * and make a list of names from it. 89 * Return the list or NULL if none found. 90 */ 91 struct name * 92 extract(char line[], int ntype) 93 { 94 char *cp; 95 struct name *begin, *np, *t; 96 char nbuf[BUFSIZ]; 97 98 if (line == NULL || *line == '\0') 99 return NULL; 100 begin = NULL; 101 np = NULL; 102 cp = line; 103 while ((cp = yankword(cp, nbuf)) != NULL) { 104 t = nalloc(nbuf, ntype); 105 if (begin == NULL) 106 begin = t; 107 else 108 np->n_flink = t; 109 t->n_blink = np; 110 np = t; 111 } 112 return begin; 113 } 114 115 /* 116 * Turn a list of names into a string of the same names. 117 */ 118 char * 119 detract(struct name *np, int ntype) 120 { 121 int s; 122 char *cp, *begin; 123 struct name *p; 124 int comma; 125 126 comma = ntype & GCOMMA; 127 if (np == NULL) 128 return(NULL); 129 ntype &= ~GCOMMA; 130 s = 0; 131 if (debug && comma) 132 fprintf(stderr, "detract asked to insert commas\n"); 133 for (p = np; p != NULL; p = p->n_flink) { 134 if (ntype && (p->n_type & GMASK) != ntype) 135 continue; 136 s += strlen(p->n_name) + 1; 137 if (comma) 138 s++; 139 } 140 if (s == 0) 141 return(NULL); 142 s += 2; 143 begin = salloc(s); 144 cp = begin; 145 for (p = np; p != NULL; p = p->n_flink) { 146 if (ntype && (p->n_type & GMASK) != ntype) 147 continue; 148 cp = copy(p->n_name, cp); 149 if (comma && p->n_flink != NULL) 150 *cp++ = ','; 151 *cp++ = ' '; 152 } 153 *--cp = 0; 154 if (comma && *--cp == ',') 155 *cp = 0; 156 return(begin); 157 } 158 159 /* 160 * Grab a single word (liberal word) 161 * Throw away things between ()'s, and take anything between <>. 162 */ 163 char * 164 yankword(char *ap, char wbuf[]) 165 { 166 char *cp, *cp2; 167 168 cp = ap; 169 for (;;) { 170 if (*cp == '\0') 171 return NULL; 172 if (*cp == '(') { 173 int nesting = 0; 174 175 while (*cp != '\0') { 176 switch (*cp++) { 177 case '(': 178 nesting++; 179 break; 180 case ')': 181 --nesting; 182 break; 183 } 184 if (nesting <= 0) 185 break; 186 } 187 } else if (*cp == ' ' || *cp == '\t' || *cp == ',') 188 cp++; 189 else 190 break; 191 } 192 if (*cp == '<') 193 for (cp2 = wbuf; *cp && (*cp2++ = *cp++) != '>';) 194 ; 195 else 196 for (cp2 = wbuf; *cp && !strchr(" \t,(", *cp); *cp2++ = *cp++) 197 ; 198 *cp2 = '\0'; 199 return cp; 200 } 201 202 /* 203 * For each recipient in the passed name list with a / 204 * in the name, append the message to the end of the named file 205 * and remove him from the recipient list. 206 * 207 * Recipients whose name begins with | are piped through the given 208 * program and removed. 209 */ 210 struct name * 211 outof(struct name *names, FILE *fo, struct header *hp) 212 { 213 int c, fd; 214 struct name *np, *begin; 215 time_t now; 216 char *date, *fname; 217 FILE *fout, *fin; 218 int ispipe; 219 char tempname[PATHSIZE]; 220 221 begin = names; 222 np = names; 223 (void)time(&now); 224 date = ctime(&now); 225 while (np != NULL) { 226 if (!isfileaddr(np->n_name) && np->n_name[0] != '|') { 227 np = np->n_flink; 228 continue; 229 } 230 ispipe = np->n_name[0] == '|'; 231 if (ispipe) 232 fname = np->n_name+1; 233 else 234 fname = expand(np->n_name); 235 236 /* 237 * See if we have copied the complete message out yet. 238 * If not, do so. 239 */ 240 241 if (image < 0) { 242 (void)snprintf(tempname, sizeof(tempname), 243 "%s/mail.ReXXXXXXXXXXXX", tmpdir); 244 if ((fd = mkstemp(tempname)) == -1 || 245 (fout = Fdopen(fd, "a")) == NULL) { 246 if (fd != -1) 247 close(fd); 248 warn("%s", tempname); 249 senderr++; 250 goto cant; 251 } 252 image = open(tempname, 2); 253 (void)unlink(tempname); 254 if (image < 0) { 255 warn("%s", tempname); 256 senderr++; 257 (void)Fclose(fout); 258 goto cant; 259 } 260 (void)fcntl(image, F_SETFD, 1); 261 fprintf(fout, "From %s %s", myname, date); 262 puthead(hp, fout, GTO|GSUBJECT|GCC|GNL); 263 while ((c = getc(fo)) != EOF) 264 (void)putc(c, fout); 265 rewind(fo); 266 (void)putc('\n', fout); 267 (void)fflush(fout); 268 if (ferror(fout)) { 269 warn("%s", tempname); 270 senderr++; 271 (void)Fclose(fout); 272 goto cant; 273 } 274 (void)Fclose(fout); 275 } 276 277 /* 278 * Now either copy "image" to the desired file 279 * or give it as the standard input to the desired 280 * program as appropriate. 281 */ 282 283 if (ispipe) { 284 int pid; 285 char *shellcmd; 286 sigset_t nset; 287 288 /* 289 * XXX 290 * We can't really reuse the same image file, 291 * because multiple piped recipients will 292 * share the same lseek location and trample 293 * on one another. 294 */ 295 if ((shellcmd = value("SHELL")) == NULL) 296 shellcmd = _PATH_CSHELL; 297 sigemptyset(&nset); 298 sigaddset(&nset, SIGHUP); 299 sigaddset(&nset, SIGINT); 300 sigaddset(&nset, SIGQUIT); 301 pid = start_command(shellcmd, &nset, 302 image, -1, "-c", fname, NULL); 303 if (pid < 0) { 304 senderr++; 305 goto cant; 306 } 307 free_child(pid); 308 } else { 309 int f; 310 if ((fout = Fopen(fname, "a")) == NULL) { 311 warn("%s", fname); 312 senderr++; 313 goto cant; 314 } 315 if ((f = dup(image)) < 0) { 316 warn("dup"); 317 fin = NULL; 318 } else 319 fin = Fdopen(f, "r"); 320 if (fin == NULL) { 321 fprintf(stderr, "Can't reopen image\n"); 322 (void)Fclose(fout); 323 senderr++; 324 goto cant; 325 } 326 rewind(fin); 327 while ((c = getc(fin)) != EOF) 328 (void)putc(c, fout); 329 if (ferror(fout)) { 330 warn("%s", fname); 331 senderr++; 332 (void)Fclose(fout); 333 (void)Fclose(fin); 334 goto cant; 335 } 336 (void)Fclose(fout); 337 (void)Fclose(fin); 338 } 339 cant: 340 /* 341 * In days of old we removed the entry from the 342 * the list; now for sake of header expansion 343 * we leave it in and mark it as deleted. 344 */ 345 np->n_type |= GDEL; 346 np = np->n_flink; 347 } 348 if (image >= 0) { 349 (void)close(image); 350 image = -1; 351 } 352 return(begin); 353 } 354 355 /* 356 * Determine if the passed address is a local "send to file" address. 357 * If any of the network metacharacters precedes any slashes, it can't 358 * be a filename. We cheat with .'s to allow path names like ./... 359 */ 360 int 361 isfileaddr(char *name) 362 { 363 char *cp; 364 365 if (*name == '+') 366 return 1; 367 for (cp = name; *cp; cp++) { 368 if (*cp == '!' || *cp == '%' || *cp == '@') 369 return 0; 370 if (*cp == '/') 371 return 1; 372 } 373 return 0; 374 } 375 376 /* 377 * Map all of the aliased users in the invoker's mailrc 378 * file and insert them into the list. 379 * Changed after all these months of service to recursively 380 * expand names (2/14/80). 381 */ 382 383 struct name * 384 usermap(struct name *names) 385 { 386 struct name *new, *np, *cp; 387 struct grouphead *gh; 388 int metoo; 389 390 new = NULL; 391 np = names; 392 metoo = (value("metoo") != NULL); 393 while (np != NULL) { 394 if (np->n_name[0] == '\\') { 395 cp = np->n_flink; 396 new = put(new, np); 397 np = cp; 398 continue; 399 } 400 gh = findgroup(np->n_name); 401 cp = np->n_flink; 402 if (gh != NULL) 403 new = gexpand(new, gh, metoo, np->n_type); 404 else 405 new = put(new, np); 406 np = cp; 407 } 408 return(new); 409 } 410 411 /* 412 * Recursively expand a group name. We limit the expansion to some 413 * fixed level to keep things from going haywire. 414 * Direct recursion is not expanded for convenience. 415 */ 416 417 struct name * 418 gexpand(struct name *nlist, struct grouphead *gh, int metoo, int ntype) 419 { 420 struct group *gp; 421 struct grouphead *ngh; 422 struct name *np; 423 static int depth; 424 char *cp; 425 426 if (depth > MAXEXP) { 427 printf("Expanding alias to depth larger than %d\n", MAXEXP); 428 return(nlist); 429 } 430 depth++; 431 for (gp = gh->g_list; gp != NULL; gp = gp->ge_link) { 432 cp = gp->ge_name; 433 if (*cp == '\\') 434 goto quote; 435 if (strcmp(cp, gh->g_name) == 0) 436 goto quote; 437 if ((ngh = findgroup(cp)) != NULL) { 438 nlist = gexpand(nlist, ngh, metoo, ntype); 439 continue; 440 } 441 quote: 442 np = nalloc(cp, ntype); 443 /* 444 * At this point should allow to expand 445 * to self if only person in group 446 */ 447 if (gp == gh->g_list && gp->ge_link == NULL) 448 goto skip; 449 if (!metoo && strcmp(cp, myname) == 0) 450 np->n_type |= GDEL; 451 skip: 452 nlist = put(nlist, np); 453 } 454 depth--; 455 return(nlist); 456 } 457 458 /* 459 * Concatenate the two passed name lists, return the result. 460 */ 461 struct name * 462 cat(struct name *n1, struct name *n2) 463 { 464 struct name *tail; 465 466 if (n1 == NULL) 467 return(n2); 468 if (n2 == NULL) 469 return(n1); 470 tail = tailof(n1); 471 tail->n_flink = n2; 472 n2->n_blink = tail; 473 return(n1); 474 } 475 476 /* 477 * Unpack the name list onto a vector of strings. 478 * Return an error if the name list won't fit. 479 */ 480 char ** 481 unpack(struct name *np) 482 { 483 char **ap, **begin; 484 struct name *n; 485 int t, extra, metoo, verbose; 486 487 n = np; 488 if ((t = count(n)) == 0) 489 errx(1, "No names to unpack"); 490 /* 491 * Compute the number of extra arguments we will need. 492 * We need at least two extra -- one for "mail" and one for 493 * the terminating 0 pointer. Additional spots may be needed 494 * to pass along -f to the host mailer. 495 */ 496 extra = 2; 497 extra++; 498 metoo = value("metoo") != NULL; 499 if (metoo) 500 extra++; 501 verbose = value("verbose") != NULL; 502 if (verbose) 503 extra++; 504 begin = (char **) salloc((t + extra) * sizeof *begin); 505 ap = begin; 506 *ap++ = "send-mail"; 507 *ap++ = "-i"; 508 if (metoo) 509 *ap++ = "-m"; 510 if (verbose) 511 *ap++ = "-v"; 512 for (; n != NULL; n = n->n_flink) 513 if ((n->n_type & GDEL) == 0) 514 *ap++ = n->n_name; 515 *ap = NULL; 516 return(begin); 517 } 518 519 /* 520 * Remove all of the duplicates from the passed name list by 521 * insertion sorting them, then checking for dups. 522 * Return the head of the new list. 523 */ 524 struct name * 525 elide(struct name *names) 526 { 527 struct name *np, *t, *new; 528 struct name *x; 529 530 if (names == NULL) 531 return(NULL); 532 new = names; 533 np = names; 534 np = np->n_flink; 535 if (np != NULL) 536 np->n_blink = NULL; 537 new->n_flink = NULL; 538 while (np != NULL) { 539 t = new; 540 while (strcasecmp(t->n_name, np->n_name) < 0) { 541 if (t->n_flink == NULL) 542 break; 543 t = t->n_flink; 544 } 545 546 /* 547 * If we ran out of t's, put the new entry after 548 * the current value of t. 549 */ 550 551 if (strcasecmp(t->n_name, np->n_name) < 0) { 552 t->n_flink = np; 553 np->n_blink = t; 554 t = np; 555 np = np->n_flink; 556 t->n_flink = NULL; 557 continue; 558 } 559 560 /* 561 * Otherwise, put the new entry in front of the 562 * current t. If at the front of the list, 563 * the new guy becomes the new head of the list. 564 */ 565 566 if (t == new) { 567 t = np; 568 np = np->n_flink; 569 t->n_flink = new; 570 new->n_blink = t; 571 t->n_blink = NULL; 572 new = t; 573 continue; 574 } 575 576 /* 577 * The normal case -- we are inserting into the 578 * middle of the list. 579 */ 580 581 x = np; 582 np = np->n_flink; 583 x->n_flink = t; 584 x->n_blink = t->n_blink; 585 t->n_blink->n_flink = x; 586 t->n_blink = x; 587 } 588 589 /* 590 * Now the list headed up by new is sorted. 591 * Go through it and remove duplicates. 592 */ 593 594 np = new; 595 while (np != NULL) { 596 t = np; 597 while (t->n_flink != NULL && 598 strcasecmp(np->n_name, t->n_flink->n_name) == 0) 599 t = t->n_flink; 600 if (t == np || t == NULL) { 601 np = np->n_flink; 602 continue; 603 } 604 605 /* 606 * Now t points to the last entry with the same name 607 * as np. Make np point beyond t. 608 */ 609 610 np->n_flink = t->n_flink; 611 if (t->n_flink != NULL) 612 t->n_flink->n_blink = np; 613 np = np->n_flink; 614 } 615 return(new); 616 } 617 618 /* 619 * Put another node onto a list of names and return 620 * the list. 621 */ 622 struct name * 623 put(struct name *list, struct name *node) 624 { 625 node->n_flink = list; 626 node->n_blink = NULL; 627 if (list != NULL) 628 list->n_blink = node; 629 return(node); 630 } 631 632 /* 633 * Determine the number of undeleted elements in 634 * a name list and return it. 635 */ 636 int 637 count(struct name *np) 638 { 639 int c; 640 641 for (c = 0; np != NULL; np = np->n_flink) 642 if ((np->n_type & GDEL) == 0) 643 c++; 644 return c; 645 } 646 647 /* 648 * Delete the given name from a namelist. 649 */ 650 struct name * 651 delname(struct name *np, char name[]) 652 { 653 struct name *p; 654 655 for (p = np; p != NULL; p = p->n_flink) 656 if (strcasecmp(p->n_name, name) == 0) { 657 if (p->n_blink == NULL) { 658 if (p->n_flink != NULL) 659 p->n_flink->n_blink = NULL; 660 np = p->n_flink; 661 continue; 662 } 663 if (p->n_flink == NULL) { 664 if (p->n_blink != NULL) 665 p->n_blink->n_flink = NULL; 666 continue; 667 } 668 p->n_blink->n_flink = p->n_flink; 669 p->n_flink->n_blink = p->n_blink; 670 } 671 return np; 672 } 673 674 /* 675 * Pretty print a name list 676 * Uncomment it if you need it. 677 */ 678 679 /* 680 void 681 prettyprint(name) 682 struct name *name; 683 { 684 struct name *np; 685 686 np = name; 687 while (np != NULL) { 688 fprintf(stderr, "%s(%d) ", np->n_name, np->n_type); 689 np = np->n_flink; 690 } 691 fprintf(stderr, "\n"); 692 } 693 */ 694