1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 23*0Sstevel@tonic-gate /* All Rights Reserved */ 24*0Sstevel@tonic-gate 25*0Sstevel@tonic-gate 26*0Sstevel@tonic-gate /* 27*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 28*0Sstevel@tonic-gate * Use is subject to license terms. 29*0Sstevel@tonic-gate */ 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gate /* 32*0Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 33*0Sstevel@tonic-gate * The Regents of the University of California 34*0Sstevel@tonic-gate * All Rights Reserved 35*0Sstevel@tonic-gate * 36*0Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 37*0Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 38*0Sstevel@tonic-gate * contributors. 39*0Sstevel@tonic-gate */ 40*0Sstevel@tonic-gate 41*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 42*0Sstevel@tonic-gate 43*0Sstevel@tonic-gate #include "rcv.h" 44*0Sstevel@tonic-gate #include <locale.h> 45*0Sstevel@tonic-gate 46*0Sstevel@tonic-gate /* 47*0Sstevel@tonic-gate * mailx -- a modified version of a University of California at Berkeley 48*0Sstevel@tonic-gate * mail program 49*0Sstevel@tonic-gate * 50*0Sstevel@tonic-gate * Mail to others. 51*0Sstevel@tonic-gate */ 52*0Sstevel@tonic-gate 53*0Sstevel@tonic-gate static void fmt(register char *str, register FILE *fo); 54*0Sstevel@tonic-gate static FILE *infix(struct header *hp, FILE *fi); 55*0Sstevel@tonic-gate static void statusput(register struct message *mp, register FILE *obuf, int doign, int (*fp)(const char *, FILE *)); 56*0Sstevel@tonic-gate static int savemail(char name[], struct header *hp, FILE *fi); 57*0Sstevel@tonic-gate static int sendmail(char *str); 58*0Sstevel@tonic-gate static int Sendmail(char *str); 59*0Sstevel@tonic-gate 60*0Sstevel@tonic-gate static off_t textpos; 61*0Sstevel@tonic-gate 62*0Sstevel@tonic-gate /* 63*0Sstevel@tonic-gate * Send message described by the passed pointer to the 64*0Sstevel@tonic-gate * passed output buffer. Return -1 on error, but normally 65*0Sstevel@tonic-gate * the number of lines written. Adjust the status: field 66*0Sstevel@tonic-gate * if need be. If doign is set, suppress ignored header fields. 67*0Sstevel@tonic-gate * Call (*fp)(line, obuf) to print the line. 68*0Sstevel@tonic-gate */ 69*0Sstevel@tonic-gate long 70*0Sstevel@tonic-gate msend( 71*0Sstevel@tonic-gate struct message *mailp, 72*0Sstevel@tonic-gate FILE *obuf, 73*0Sstevel@tonic-gate int flag, 74*0Sstevel@tonic-gate int (*fp)(const char *, FILE *)) 75*0Sstevel@tonic-gate { 76*0Sstevel@tonic-gate register struct message *mp; 77*0Sstevel@tonic-gate long clen, n, c; 78*0Sstevel@tonic-gate FILE *ibuf; 79*0Sstevel@tonic-gate char line[LINESIZE], field[BUFSIZ]; 80*0Sstevel@tonic-gate int ishead, infld, fline, dostat, doclen, nread, unused; 81*0Sstevel@tonic-gate char *cp, *cp2; 82*0Sstevel@tonic-gate int doign = flag & M_IGNORE; 83*0Sstevel@tonic-gate int oldign = 0; /* previous line was ignored */ 84*0Sstevel@tonic-gate long lc; 85*0Sstevel@tonic-gate 86*0Sstevel@tonic-gate mp = mailp; 87*0Sstevel@tonic-gate if (mp->m_clen == 0) 88*0Sstevel@tonic-gate setclen(mp); 89*0Sstevel@tonic-gate ibuf = setinput(mp); 90*0Sstevel@tonic-gate c = mp->m_size; 91*0Sstevel@tonic-gate ishead = 1; 92*0Sstevel@tonic-gate dostat = 1; 93*0Sstevel@tonic-gate doclen = 1; 94*0Sstevel@tonic-gate infld = 0; 95*0Sstevel@tonic-gate fline = 1; 96*0Sstevel@tonic-gate lc = 0; 97*0Sstevel@tonic-gate clearerr(obuf); 98*0Sstevel@tonic-gate while (c > 0L) { 99*0Sstevel@tonic-gate nread = getline(line, LINESIZE, ibuf, &unused); 100*0Sstevel@tonic-gate c -= nread; 101*0Sstevel@tonic-gate lc++; 102*0Sstevel@tonic-gate if (ishead) { 103*0Sstevel@tonic-gate /* 104*0Sstevel@tonic-gate * First line is the From line, so no headers 105*0Sstevel@tonic-gate * there to worry about 106*0Sstevel@tonic-gate */ 107*0Sstevel@tonic-gate if (fline) { 108*0Sstevel@tonic-gate fline = 0; 109*0Sstevel@tonic-gate goto writeit; 110*0Sstevel@tonic-gate } 111*0Sstevel@tonic-gate /* 112*0Sstevel@tonic-gate * If line is blank, we've reached end of 113*0Sstevel@tonic-gate * headers, so force out status: field 114*0Sstevel@tonic-gate * and note that we are no longer in header 115*0Sstevel@tonic-gate * fields. Also force out Content-Length: field. 116*0Sstevel@tonic-gate */ 117*0Sstevel@tonic-gate if (line[0] == '\n') { 118*0Sstevel@tonic-gate if (dostat) { 119*0Sstevel@tonic-gate statusput(mailp, obuf, doign, fp); 120*0Sstevel@tonic-gate dostat = 0; 121*0Sstevel@tonic-gate } 122*0Sstevel@tonic-gate if (doclen && 123*0Sstevel@tonic-gate !isign("content-length", flag&M_SAVING)) { 124*0Sstevel@tonic-gate snprintf(field, sizeof (field), 125*0Sstevel@tonic-gate "Content-Length: %ld\n", 126*0Sstevel@tonic-gate mp->m_clen - 1); 127*0Sstevel@tonic-gate (*fp)(field, obuf); 128*0Sstevel@tonic-gate if (ferror(obuf)) 129*0Sstevel@tonic-gate return(-1); 130*0Sstevel@tonic-gate doclen = 0; 131*0Sstevel@tonic-gate } 132*0Sstevel@tonic-gate ishead = 0; 133*0Sstevel@tonic-gate goto writeit; 134*0Sstevel@tonic-gate } 135*0Sstevel@tonic-gate /* 136*0Sstevel@tonic-gate * If this line is a continuation 137*0Sstevel@tonic-gate * of a previous header field, just echo it. 138*0Sstevel@tonic-gate */ 139*0Sstevel@tonic-gate if (isspace(line[0]) && infld) 140*0Sstevel@tonic-gate if (oldign) 141*0Sstevel@tonic-gate continue; 142*0Sstevel@tonic-gate else 143*0Sstevel@tonic-gate goto writeit; 144*0Sstevel@tonic-gate infld = 0; 145*0Sstevel@tonic-gate /* 146*0Sstevel@tonic-gate * If we are no longer looking at real 147*0Sstevel@tonic-gate * header lines, force out status: 148*0Sstevel@tonic-gate * This happens in uucp style mail where 149*0Sstevel@tonic-gate * there are no headers at all. 150*0Sstevel@tonic-gate */ 151*0Sstevel@tonic-gate if (!headerp(line)) { 152*0Sstevel@tonic-gate if (dostat) { 153*0Sstevel@tonic-gate statusput(mailp, obuf, doign, fp); 154*0Sstevel@tonic-gate dostat = 0; 155*0Sstevel@tonic-gate } 156*0Sstevel@tonic-gate (*fp)("\n", obuf); 157*0Sstevel@tonic-gate ishead = 0; 158*0Sstevel@tonic-gate goto writeit; 159*0Sstevel@tonic-gate } 160*0Sstevel@tonic-gate infld++; 161*0Sstevel@tonic-gate /* 162*0Sstevel@tonic-gate * Pick up the header field. 163*0Sstevel@tonic-gate * If it is an ignored field and 164*0Sstevel@tonic-gate * we care about such things, skip it. 165*0Sstevel@tonic-gate */ 166*0Sstevel@tonic-gate cp = line; 167*0Sstevel@tonic-gate cp2 = field; 168*0Sstevel@tonic-gate while (*cp && *cp != ':' && !isspace(*cp)) 169*0Sstevel@tonic-gate *cp2++ = *cp++; 170*0Sstevel@tonic-gate *cp2 = 0; 171*0Sstevel@tonic-gate oldign = doign && isign(field, flag&M_SAVING); 172*0Sstevel@tonic-gate if (oldign) 173*0Sstevel@tonic-gate continue; 174*0Sstevel@tonic-gate /* 175*0Sstevel@tonic-gate * If the field is "status," go compute and print the 176*0Sstevel@tonic-gate * real Status: field 177*0Sstevel@tonic-gate */ 178*0Sstevel@tonic-gate if (icequal(field, "status")) { 179*0Sstevel@tonic-gate if (dostat) { 180*0Sstevel@tonic-gate statusput(mailp, obuf, doign, fp); 181*0Sstevel@tonic-gate dostat = 0; 182*0Sstevel@tonic-gate } 183*0Sstevel@tonic-gate continue; 184*0Sstevel@tonic-gate } 185*0Sstevel@tonic-gate if (icequal(field, "content-length")) { 186*0Sstevel@tonic-gate if (doclen) { 187*0Sstevel@tonic-gate snprintf(line, sizeof (line), 188*0Sstevel@tonic-gate "Content-Length: %ld\n", 189*0Sstevel@tonic-gate mp->m_clen - 1); 190*0Sstevel@tonic-gate (*fp)(line, obuf); 191*0Sstevel@tonic-gate if (ferror(obuf)) 192*0Sstevel@tonic-gate return(-1); 193*0Sstevel@tonic-gate doclen = 0; 194*0Sstevel@tonic-gate } 195*0Sstevel@tonic-gate continue; 196*0Sstevel@tonic-gate } 197*0Sstevel@tonic-gate } 198*0Sstevel@tonic-gate writeit: 199*0Sstevel@tonic-gate if (!ishead && !mp->m_text && mp->m_clen != 0) { 200*0Sstevel@tonic-gate if (line[0] == '\n') 201*0Sstevel@tonic-gate putc('\n', obuf); 202*0Sstevel@tonic-gate clen = mp->m_clen-1; 203*0Sstevel@tonic-gate for (;;) { 204*0Sstevel@tonic-gate n = clen < sizeof line ? clen : sizeof line; 205*0Sstevel@tonic-gate if ((n = fread(line, 1, (int)n, ibuf)) <= 0) { 206*0Sstevel@tonic-gate fprintf(stderr, gettext( 207*0Sstevel@tonic-gate "\t(Unexpected end-of-file).\n")); 208*0Sstevel@tonic-gate clen = 0; 209*0Sstevel@tonic-gate } else { 210*0Sstevel@tonic-gate if (fwrite(line, 1, (int)n, obuf) != n) { 211*0Sstevel@tonic-gate fprintf(stderr, gettext( 212*0Sstevel@tonic-gate "\tError writing to the new file.\n")); 213*0Sstevel@tonic-gate fflush(obuf); 214*0Sstevel@tonic-gate if (fferror(obuf)) 215*0Sstevel@tonic-gate return (-1); 216*0Sstevel@tonic-gate } 217*0Sstevel@tonic-gate } 218*0Sstevel@tonic-gate clen -= n; 219*0Sstevel@tonic-gate if (clen <= 0) { 220*0Sstevel@tonic-gate break; 221*0Sstevel@tonic-gate } 222*0Sstevel@tonic-gate } 223*0Sstevel@tonic-gate c = 0L; 224*0Sstevel@tonic-gate } else { 225*0Sstevel@tonic-gate (*fp)(line, obuf); 226*0Sstevel@tonic-gate if (ferror(obuf)) 227*0Sstevel@tonic-gate return(-1); 228*0Sstevel@tonic-gate } 229*0Sstevel@tonic-gate } 230*0Sstevel@tonic-gate fflush(obuf); 231*0Sstevel@tonic-gate if (ferror(obuf)) 232*0Sstevel@tonic-gate return(-1); 233*0Sstevel@tonic-gate if (ishead && (mailp->m_flag & MSTATUS)) 234*0Sstevel@tonic-gate printf(gettext("failed to fix up status field\n")); 235*0Sstevel@tonic-gate return(lc); 236*0Sstevel@tonic-gate } 237*0Sstevel@tonic-gate 238*0Sstevel@tonic-gate /* 239*0Sstevel@tonic-gate * Test if the passed line is a header line, RFC 822 style. 240*0Sstevel@tonic-gate */ 241*0Sstevel@tonic-gate int 242*0Sstevel@tonic-gate headerp(register char *line) 243*0Sstevel@tonic-gate { 244*0Sstevel@tonic-gate register char *cp = line; 245*0Sstevel@tonic-gate 246*0Sstevel@tonic-gate while (*cp && *cp != ' ' && *cp != '\t' && *cp != ':') 247*0Sstevel@tonic-gate cp++; 248*0Sstevel@tonic-gate return(*cp == ':'); 249*0Sstevel@tonic-gate } 250*0Sstevel@tonic-gate 251*0Sstevel@tonic-gate /* 252*0Sstevel@tonic-gate * Output a reasonable looking status field. 253*0Sstevel@tonic-gate * But if "status" is ignored and doign, forget it. 254*0Sstevel@tonic-gate */ 255*0Sstevel@tonic-gate static void 256*0Sstevel@tonic-gate statusput( 257*0Sstevel@tonic-gate register struct message *mp, 258*0Sstevel@tonic-gate register FILE *obuf, 259*0Sstevel@tonic-gate int doign, 260*0Sstevel@tonic-gate int (*fp)(const char *, FILE *)) 261*0Sstevel@tonic-gate { 262*0Sstevel@tonic-gate char statout[12]; 263*0Sstevel@tonic-gate 264*0Sstevel@tonic-gate if (doign && isign("status", 0)) 265*0Sstevel@tonic-gate return; 266*0Sstevel@tonic-gate if ((mp->m_flag & (MNEW|MREAD)) == MNEW) 267*0Sstevel@tonic-gate return; 268*0Sstevel@tonic-gate strcpy(statout, "Status: "); 269*0Sstevel@tonic-gate if (mp->m_flag & MREAD) 270*0Sstevel@tonic-gate strcat(statout, "R"); 271*0Sstevel@tonic-gate if ((mp->m_flag & MNEW) == 0) 272*0Sstevel@tonic-gate strcat(statout, "O"); 273*0Sstevel@tonic-gate strcat(statout, "\n"); 274*0Sstevel@tonic-gate (*fp)(statout, obuf); 275*0Sstevel@tonic-gate } 276*0Sstevel@tonic-gate 277*0Sstevel@tonic-gate /* 278*0Sstevel@tonic-gate * Interface between the argument list and the mail1 routine 279*0Sstevel@tonic-gate * which does all the dirty work. 280*0Sstevel@tonic-gate */ 281*0Sstevel@tonic-gate 282*0Sstevel@tonic-gate int 283*0Sstevel@tonic-gate mail(char **people) 284*0Sstevel@tonic-gate { 285*0Sstevel@tonic-gate register char *cp2, *cp3; 286*0Sstevel@tonic-gate register int s; 287*0Sstevel@tonic-gate char *buf, **ap; 288*0Sstevel@tonic-gate struct header head; 289*0Sstevel@tonic-gate 290*0Sstevel@tonic-gate for (s = 0, ap = people; *ap; ap++) 291*0Sstevel@tonic-gate s += strlen(*ap) + 2; 292*0Sstevel@tonic-gate buf = (char *)salloc((unsigned)(s+1)); 293*0Sstevel@tonic-gate cp2 = buf; 294*0Sstevel@tonic-gate for (ap = people; *ap; ap++) { 295*0Sstevel@tonic-gate for (cp3 = *ap; *cp3; ) { 296*0Sstevel@tonic-gate if (*cp3 == ' ' || *cp3 == '\t') { 297*0Sstevel@tonic-gate *cp3++ = ','; 298*0Sstevel@tonic-gate while (*cp3 == ' ' || *cp3 == '\t') 299*0Sstevel@tonic-gate cp3++; 300*0Sstevel@tonic-gate } else 301*0Sstevel@tonic-gate cp3++; 302*0Sstevel@tonic-gate } 303*0Sstevel@tonic-gate cp2 = copy(*ap, cp2); 304*0Sstevel@tonic-gate *cp2++ = ','; 305*0Sstevel@tonic-gate *cp2++ = ' '; 306*0Sstevel@tonic-gate } 307*0Sstevel@tonic-gate *cp2 = '\0'; 308*0Sstevel@tonic-gate head.h_to = buf; 309*0Sstevel@tonic-gate head.h_subject = head.h_cc = head.h_bcc = head.h_defopt = NOSTR; 310*0Sstevel@tonic-gate head.h_others = NOSTRPTR; 311*0Sstevel@tonic-gate head.h_seq = 0; 312*0Sstevel@tonic-gate mail1(&head, Fflag, NOSTR); 313*0Sstevel@tonic-gate return(0); 314*0Sstevel@tonic-gate } 315*0Sstevel@tonic-gate 316*0Sstevel@tonic-gate int 317*0Sstevel@tonic-gate sendm(char *str) 318*0Sstevel@tonic-gate { 319*0Sstevel@tonic-gate if (value("flipm") != NOSTR) 320*0Sstevel@tonic-gate return(Sendmail(str)); 321*0Sstevel@tonic-gate else return(sendmail(str)); 322*0Sstevel@tonic-gate } 323*0Sstevel@tonic-gate 324*0Sstevel@tonic-gate int 325*0Sstevel@tonic-gate Sendm(char *str) 326*0Sstevel@tonic-gate { 327*0Sstevel@tonic-gate if (value("flipm") != NOSTR) 328*0Sstevel@tonic-gate return(sendmail(str)); 329*0Sstevel@tonic-gate else return(Sendmail(str)); 330*0Sstevel@tonic-gate } 331*0Sstevel@tonic-gate 332*0Sstevel@tonic-gate /* 333*0Sstevel@tonic-gate * Interface to the mail1 routine for the -t flag 334*0Sstevel@tonic-gate * (read headers from text). 335*0Sstevel@tonic-gate */ 336*0Sstevel@tonic-gate int 337*0Sstevel@tonic-gate tmail(void) 338*0Sstevel@tonic-gate { 339*0Sstevel@tonic-gate struct header head; 340*0Sstevel@tonic-gate 341*0Sstevel@tonic-gate head.h_to = NOSTR; 342*0Sstevel@tonic-gate head.h_subject = head.h_cc = head.h_bcc = head.h_defopt = NOSTR; 343*0Sstevel@tonic-gate head.h_others = NOSTRPTR; 344*0Sstevel@tonic-gate head.h_seq = 0; 345*0Sstevel@tonic-gate mail1(&head, Fflag, NOSTR); 346*0Sstevel@tonic-gate return(0); 347*0Sstevel@tonic-gate } 348*0Sstevel@tonic-gate 349*0Sstevel@tonic-gate /* 350*0Sstevel@tonic-gate * Send mail to a bunch of user names. The interface is through 351*0Sstevel@tonic-gate * the mail routine below. 352*0Sstevel@tonic-gate */ 353*0Sstevel@tonic-gate static int 354*0Sstevel@tonic-gate sendmail(char *str) 355*0Sstevel@tonic-gate { 356*0Sstevel@tonic-gate struct header head; 357*0Sstevel@tonic-gate 358*0Sstevel@tonic-gate if (blankline(str)) 359*0Sstevel@tonic-gate head.h_to = NOSTR; 360*0Sstevel@tonic-gate else 361*0Sstevel@tonic-gate head.h_to = addto(NOSTR, str); 362*0Sstevel@tonic-gate head.h_subject = head.h_cc = head.h_bcc = head.h_defopt = NOSTR; 363*0Sstevel@tonic-gate head.h_others = NOSTRPTR; 364*0Sstevel@tonic-gate head.h_seq = 0; 365*0Sstevel@tonic-gate mail1(&head, 0, NOSTR); 366*0Sstevel@tonic-gate return(0); 367*0Sstevel@tonic-gate } 368*0Sstevel@tonic-gate 369*0Sstevel@tonic-gate /* 370*0Sstevel@tonic-gate * Send mail to a bunch of user names. The interface is through 371*0Sstevel@tonic-gate * the mail routine below. 372*0Sstevel@tonic-gate * save a copy of the letter 373*0Sstevel@tonic-gate */ 374*0Sstevel@tonic-gate static int 375*0Sstevel@tonic-gate Sendmail(char *str) 376*0Sstevel@tonic-gate { 377*0Sstevel@tonic-gate struct header head; 378*0Sstevel@tonic-gate 379*0Sstevel@tonic-gate if (blankline(str)) 380*0Sstevel@tonic-gate head.h_to = NOSTR; 381*0Sstevel@tonic-gate else 382*0Sstevel@tonic-gate head.h_to = addto(NOSTR, str); 383*0Sstevel@tonic-gate head.h_subject = head.h_cc = head.h_bcc = head.h_defopt = NOSTR; 384*0Sstevel@tonic-gate head.h_others = NOSTRPTR; 385*0Sstevel@tonic-gate head.h_seq = 0; 386*0Sstevel@tonic-gate mail1(&head, 1, NOSTR); 387*0Sstevel@tonic-gate return(0); 388*0Sstevel@tonic-gate } 389*0Sstevel@tonic-gate 390*0Sstevel@tonic-gate /* 391*0Sstevel@tonic-gate * Walk the list of fds, closing all but one. 392*0Sstevel@tonic-gate */ 393*0Sstevel@tonic-gate static int 394*0Sstevel@tonic-gate closefd_walk(void *special_fd, int fd) 395*0Sstevel@tonic-gate { 396*0Sstevel@tonic-gate if (fd > STDERR_FILENO && fd != *(int *)special_fd) 397*0Sstevel@tonic-gate (void) close(fd); 398*0Sstevel@tonic-gate return (0); 399*0Sstevel@tonic-gate } 400*0Sstevel@tonic-gate 401*0Sstevel@tonic-gate /* 402*0Sstevel@tonic-gate * Mail a message on standard input to the people indicated 403*0Sstevel@tonic-gate * in the passed header. (Internal interface). 404*0Sstevel@tonic-gate */ 405*0Sstevel@tonic-gate void 406*0Sstevel@tonic-gate mail1(struct header *hp, int use_to, char *orig_to) 407*0Sstevel@tonic-gate { 408*0Sstevel@tonic-gate pid_t p, pid; 409*0Sstevel@tonic-gate int i, s, gotcha; 410*0Sstevel@tonic-gate char **namelist, *deliver; 411*0Sstevel@tonic-gate struct name *to, *np; 412*0Sstevel@tonic-gate FILE *mtf, *fp; 413*0Sstevel@tonic-gate int remote = rflag != NOSTR || rmail; 414*0Sstevel@tonic-gate char **t; 415*0Sstevel@tonic-gate char *deadletter; 416*0Sstevel@tonic-gate char recfile[PATHSIZE]; 417*0Sstevel@tonic-gate 418*0Sstevel@tonic-gate /* 419*0Sstevel@tonic-gate * Collect user's mail from standard input. 420*0Sstevel@tonic-gate * Get the result as mtf. 421*0Sstevel@tonic-gate */ 422*0Sstevel@tonic-gate 423*0Sstevel@tonic-gate pid = (pid_t)-1; 424*0Sstevel@tonic-gate if ((mtf = collect(hp)) == NULL) 425*0Sstevel@tonic-gate return; 426*0Sstevel@tonic-gate hp->h_seq = 1; 427*0Sstevel@tonic-gate if (hp->h_subject == NOSTR) 428*0Sstevel@tonic-gate hp->h_subject = sflag; 429*0Sstevel@tonic-gate if (fsize(mtf) == 0 && hp->h_subject == NOSTR) { 430*0Sstevel@tonic-gate printf(gettext("No message !?!\n")); 431*0Sstevel@tonic-gate goto out; 432*0Sstevel@tonic-gate } 433*0Sstevel@tonic-gate if (intty) { 434*0Sstevel@tonic-gate printf(gettext("EOT\n")); 435*0Sstevel@tonic-gate flush(); 436*0Sstevel@tonic-gate } 437*0Sstevel@tonic-gate 438*0Sstevel@tonic-gate /* 439*0Sstevel@tonic-gate * If we need to use the To: line to determine the record 440*0Sstevel@tonic-gate * file, save a copy of it before it's sorted below. 441*0Sstevel@tonic-gate */ 442*0Sstevel@tonic-gate 443*0Sstevel@tonic-gate if (use_to && orig_to == NOSTR && hp->h_to != NOSTR) 444*0Sstevel@tonic-gate orig_to = strcpy((char *)salloc(strlen(hp->h_to)+1), hp->h_to); 445*0Sstevel@tonic-gate else if (orig_to == NOSTR) 446*0Sstevel@tonic-gate orig_to = ""; 447*0Sstevel@tonic-gate 448*0Sstevel@tonic-gate /* 449*0Sstevel@tonic-gate * Now, take the user names from the combined 450*0Sstevel@tonic-gate * to and cc lists and do all the alias 451*0Sstevel@tonic-gate * processing. 452*0Sstevel@tonic-gate */ 453*0Sstevel@tonic-gate 454*0Sstevel@tonic-gate senderr = 0; 455*0Sstevel@tonic-gate to = cat(extract(hp->h_bcc, GBCC), 456*0Sstevel@tonic-gate cat(extract(hp->h_to, GTO), 457*0Sstevel@tonic-gate extract(hp->h_cc, GCC))); 458*0Sstevel@tonic-gate to = translate(outpre(elide(usermap(to)))); 459*0Sstevel@tonic-gate if (!senderr) 460*0Sstevel@tonic-gate mapf(to, myname); 461*0Sstevel@tonic-gate mechk(to); 462*0Sstevel@tonic-gate for (gotcha = 0, np = to; np != NIL; np = np->n_flink) 463*0Sstevel@tonic-gate if ((np->n_type & GDEL) == 0) 464*0Sstevel@tonic-gate gotcha++; 465*0Sstevel@tonic-gate hp->h_to = detract(to, GTO); 466*0Sstevel@tonic-gate hp->h_cc = detract(to, GCC); 467*0Sstevel@tonic-gate hp->h_bcc = detract(to, GBCC); 468*0Sstevel@tonic-gate if ((mtf = infix(hp, mtf)) == NULL) { 469*0Sstevel@tonic-gate fprintf(stderr, gettext(". . . message lost, sorry.\n")); 470*0Sstevel@tonic-gate return; 471*0Sstevel@tonic-gate } 472*0Sstevel@tonic-gate rewind(mtf); 473*0Sstevel@tonic-gate if (askme && isatty(0)) { 474*0Sstevel@tonic-gate char ans[64]; 475*0Sstevel@tonic-gate puthead(hp, stdout, GTO|GCC|GBCC, 0); 476*0Sstevel@tonic-gate printf(gettext("Send? ")); 477*0Sstevel@tonic-gate printf("[yes] "); 478*0Sstevel@tonic-gate if (fgets(ans, sizeof(ans), stdin) && ans[0] && 479*0Sstevel@tonic-gate (tolower(ans[0]) != 'y' && ans[0] != '\n')) 480*0Sstevel@tonic-gate goto dead; 481*0Sstevel@tonic-gate } 482*0Sstevel@tonic-gate if (senderr) 483*0Sstevel@tonic-gate goto dead; 484*0Sstevel@tonic-gate /* 485*0Sstevel@tonic-gate * Look through the recipient list for names with /'s 486*0Sstevel@tonic-gate * in them which we write to as files directly. 487*0Sstevel@tonic-gate */ 488*0Sstevel@tonic-gate i = outof(to, mtf); 489*0Sstevel@tonic-gate rewind(mtf); 490*0Sstevel@tonic-gate if (!gotcha && !i) { 491*0Sstevel@tonic-gate printf(gettext("No recipients specified\n")); 492*0Sstevel@tonic-gate goto dead; 493*0Sstevel@tonic-gate } 494*0Sstevel@tonic-gate if (senderr) 495*0Sstevel@tonic-gate goto dead; 496*0Sstevel@tonic-gate 497*0Sstevel@tonic-gate getrecf(orig_to, recfile, use_to, sizeof (recfile)); 498*0Sstevel@tonic-gate if (recfile != NOSTR && *recfile) 499*0Sstevel@tonic-gate savemail(safeexpand(recfile), hp, mtf); 500*0Sstevel@tonic-gate if (!gotcha) 501*0Sstevel@tonic-gate goto out; 502*0Sstevel@tonic-gate namelist = unpack(to); 503*0Sstevel@tonic-gate if (debug) { 504*0Sstevel@tonic-gate fprintf(stderr, "Recipients of message:\n"); 505*0Sstevel@tonic-gate for (t = namelist; *t != NOSTR; t++) 506*0Sstevel@tonic-gate fprintf(stderr, " \"%s\"", *t); 507*0Sstevel@tonic-gate fprintf(stderr, "\n"); 508*0Sstevel@tonic-gate return; 509*0Sstevel@tonic-gate } 510*0Sstevel@tonic-gate 511*0Sstevel@tonic-gate /* 512*0Sstevel@tonic-gate * Wait, to absorb a potential zombie, then 513*0Sstevel@tonic-gate * fork, set up the temporary mail file as standard 514*0Sstevel@tonic-gate * input for "mail" and exec with the user list we generated 515*0Sstevel@tonic-gate * far above. Return the process id to caller in case he 516*0Sstevel@tonic-gate * wants to await the completion of mail. 517*0Sstevel@tonic-gate */ 518*0Sstevel@tonic-gate 519*0Sstevel@tonic-gate #ifdef VMUNIX 520*0Sstevel@tonic-gate while (wait3((int *)0, WNOHANG, (struct rusage *)0) > 0) 521*0Sstevel@tonic-gate ; 522*0Sstevel@tonic-gate #else 523*0Sstevel@tonic-gate #ifdef preSVr4 524*0Sstevel@tonic-gate wait((int *)0); 525*0Sstevel@tonic-gate #else 526*0Sstevel@tonic-gate while (waitpid((pid_t)-1, (int *)0, WNOHANG) > 0) 527*0Sstevel@tonic-gate ; 528*0Sstevel@tonic-gate #endif 529*0Sstevel@tonic-gate #endif 530*0Sstevel@tonic-gate rewind(mtf); 531*0Sstevel@tonic-gate pid = fork(); 532*0Sstevel@tonic-gate if (pid == (pid_t)-1) { 533*0Sstevel@tonic-gate perror("fork"); 534*0Sstevel@tonic-gate dead: 535*0Sstevel@tonic-gate deadletter = Getf("DEAD"); 536*0Sstevel@tonic-gate if (fp = fopen(deadletter, 537*0Sstevel@tonic-gate value("appenddeadletter") == NOSTR ? "w" : "a")) { 538*0Sstevel@tonic-gate chmod(deadletter, DEADPERM); 539*0Sstevel@tonic-gate puthead(hp, fp, GMASK|GCLEN, fsize(mtf) - textpos); 540*0Sstevel@tonic-gate fseek(mtf, textpos, 0); 541*0Sstevel@tonic-gate lcwrite(deadletter, mtf, fp, 542*0Sstevel@tonic-gate value("appenddeadletter") != NOSTR); 543*0Sstevel@tonic-gate fclose(fp); 544*0Sstevel@tonic-gate } else 545*0Sstevel@tonic-gate perror(deadletter); 546*0Sstevel@tonic-gate goto out; 547*0Sstevel@tonic-gate } 548*0Sstevel@tonic-gate if (pid == 0) { 549*0Sstevel@tonic-gate sigchild(); 550*0Sstevel@tonic-gate #ifdef SIGTSTP 551*0Sstevel@tonic-gate if (remote == 0) { 552*0Sstevel@tonic-gate sigset(SIGTSTP, SIG_IGN); 553*0Sstevel@tonic-gate sigset(SIGTTIN, SIG_IGN); 554*0Sstevel@tonic-gate sigset(SIGTTOU, SIG_IGN); 555*0Sstevel@tonic-gate } 556*0Sstevel@tonic-gate #endif 557*0Sstevel@tonic-gate sigset(SIGHUP, SIG_IGN); 558*0Sstevel@tonic-gate sigset(SIGINT, SIG_IGN); 559*0Sstevel@tonic-gate sigset(SIGQUIT, SIG_IGN); 560*0Sstevel@tonic-gate s = fileno(mtf); 561*0Sstevel@tonic-gate (void) fdwalk(closefd_walk, &s); 562*0Sstevel@tonic-gate close(0); 563*0Sstevel@tonic-gate dup(s); 564*0Sstevel@tonic-gate close(s); 565*0Sstevel@tonic-gate #ifdef CC 566*0Sstevel@tonic-gate submit(getpid()); 567*0Sstevel@tonic-gate #endif /* CC */ 568*0Sstevel@tonic-gate if ((deliver = value("sendmail")) == NOSTR) 569*0Sstevel@tonic-gate #ifdef SENDMAIL 570*0Sstevel@tonic-gate deliver = SENDMAIL; 571*0Sstevel@tonic-gate #else 572*0Sstevel@tonic-gate deliver = MAIL; 573*0Sstevel@tonic-gate #endif 574*0Sstevel@tonic-gate execvp(safeexpand(deliver), namelist); 575*0Sstevel@tonic-gate perror(deliver); 576*0Sstevel@tonic-gate exit(1); 577*0Sstevel@tonic-gate } 578*0Sstevel@tonic-gate 579*0Sstevel@tonic-gate if (value("sendwait")!=NOSTR) 580*0Sstevel@tonic-gate remote++; 581*0Sstevel@tonic-gate out: 582*0Sstevel@tonic-gate if (remote) { 583*0Sstevel@tonic-gate while ((p = wait(&s)) != pid && p != (pid_t)-1) 584*0Sstevel@tonic-gate ; 585*0Sstevel@tonic-gate if (s != 0) 586*0Sstevel@tonic-gate senderr++; 587*0Sstevel@tonic-gate pid = 0; 588*0Sstevel@tonic-gate } 589*0Sstevel@tonic-gate fclose(mtf); 590*0Sstevel@tonic-gate return; 591*0Sstevel@tonic-gate } 592*0Sstevel@tonic-gate 593*0Sstevel@tonic-gate /* 594*0Sstevel@tonic-gate * Prepend a header in front of the collected stuff 595*0Sstevel@tonic-gate * and return the new file. 596*0Sstevel@tonic-gate */ 597*0Sstevel@tonic-gate 598*0Sstevel@tonic-gate static FILE * 599*0Sstevel@tonic-gate infix(struct header *hp, FILE *fi) 600*0Sstevel@tonic-gate { 601*0Sstevel@tonic-gate register FILE *nfo, *nfi; 602*0Sstevel@tonic-gate register int c; 603*0Sstevel@tonic-gate char *postmark, *returnaddr; 604*0Sstevel@tonic-gate int fd = -1; 605*0Sstevel@tonic-gate 606*0Sstevel@tonic-gate rewind(fi); 607*0Sstevel@tonic-gate if ((fd = open(tempMail, O_RDWR|O_CREAT|O_EXCL, 0600)) < 0 || 608*0Sstevel@tonic-gate (nfo = fdopen(fd, "w")) == NULL) { 609*0Sstevel@tonic-gate perror(tempMail); 610*0Sstevel@tonic-gate return(fi); 611*0Sstevel@tonic-gate } 612*0Sstevel@tonic-gate if ((nfi = fopen(tempMail, "r")) == NULL) { 613*0Sstevel@tonic-gate perror(tempMail); 614*0Sstevel@tonic-gate fclose(nfo); 615*0Sstevel@tonic-gate return(fi); 616*0Sstevel@tonic-gate } 617*0Sstevel@tonic-gate removefile(tempMail); 618*0Sstevel@tonic-gate postmark = value("postmark"); 619*0Sstevel@tonic-gate returnaddr = value("returnaddr"); 620*0Sstevel@tonic-gate if ((postmark != NOSTR) || (returnaddr != NOSTR)) { 621*0Sstevel@tonic-gate if (returnaddr && *returnaddr) 622*0Sstevel@tonic-gate fprintf(nfo, "From: %s", returnaddr); 623*0Sstevel@tonic-gate else 624*0Sstevel@tonic-gate fprintf(nfo, "From: %s@%s", myname, host); 625*0Sstevel@tonic-gate if (postmark && *postmark) 626*0Sstevel@tonic-gate fprintf(nfo, " (%s)", postmark); 627*0Sstevel@tonic-gate putc('\n', nfo); 628*0Sstevel@tonic-gate } 629*0Sstevel@tonic-gate puthead(hp, nfo, (GMASK & ~GBCC) | GCLEN, fsize(fi)); 630*0Sstevel@tonic-gate textpos = ftell(nfo); 631*0Sstevel@tonic-gate while ((c = getc(fi)) != EOF) 632*0Sstevel@tonic-gate putc(c, nfo); 633*0Sstevel@tonic-gate if (ferror(fi)) { 634*0Sstevel@tonic-gate perror("read"); 635*0Sstevel@tonic-gate return(fi); 636*0Sstevel@tonic-gate } 637*0Sstevel@tonic-gate fflush(nfo); 638*0Sstevel@tonic-gate if (fferror(nfo)) { 639*0Sstevel@tonic-gate perror(tempMail); 640*0Sstevel@tonic-gate fclose(nfo); 641*0Sstevel@tonic-gate fclose(nfi); 642*0Sstevel@tonic-gate return(fi); 643*0Sstevel@tonic-gate } 644*0Sstevel@tonic-gate fclose(nfo); 645*0Sstevel@tonic-gate fclose(fi); 646*0Sstevel@tonic-gate rewind(nfi); 647*0Sstevel@tonic-gate return(nfi); 648*0Sstevel@tonic-gate } 649*0Sstevel@tonic-gate 650*0Sstevel@tonic-gate /* 651*0Sstevel@tonic-gate * Dump the message header on the 652*0Sstevel@tonic-gate * passed file buffer. 653*0Sstevel@tonic-gate */ 654*0Sstevel@tonic-gate 655*0Sstevel@tonic-gate puthead(struct header *hp, FILE *fo, int w, long clen) 656*0Sstevel@tonic-gate { 657*0Sstevel@tonic-gate register int gotcha; 658*0Sstevel@tonic-gate 659*0Sstevel@tonic-gate gotcha = 0; 660*0Sstevel@tonic-gate if (hp->h_to != NOSTR && (w & GTO)) 661*0Sstevel@tonic-gate fprintf(fo, "To: "), fmt(hp->h_to, fo), gotcha++; 662*0Sstevel@tonic-gate if ((w & GSUBJECT) && (int)value("bsdcompat")) 663*0Sstevel@tonic-gate if (hp->h_subject != NOSTR && *hp->h_subject) 664*0Sstevel@tonic-gate fprintf(fo, "Subject: %s\n", hp->h_subject), gotcha++; 665*0Sstevel@tonic-gate else 666*0Sstevel@tonic-gate if (sflag && *sflag) 667*0Sstevel@tonic-gate fprintf(fo, "Subject: %s\n", sflag), gotcha++; 668*0Sstevel@tonic-gate if (hp->h_cc != NOSTR && (w & GCC)) 669*0Sstevel@tonic-gate fprintf(fo, "Cc: "), fmt(hp->h_cc, fo), gotcha++; 670*0Sstevel@tonic-gate if (hp->h_bcc != NOSTR && (w & GBCC)) 671*0Sstevel@tonic-gate fprintf(fo, "Bcc: "), fmt(hp->h_bcc, fo), gotcha++; 672*0Sstevel@tonic-gate if (hp->h_defopt != NOSTR && (w & GDEFOPT)) 673*0Sstevel@tonic-gate if (receipt_flg) 674*0Sstevel@tonic-gate fprintf(fo, "Return-Receipt-To: %s\n", 675*0Sstevel@tonic-gate hp->h_defopt), gotcha++; 676*0Sstevel@tonic-gate else 677*0Sstevel@tonic-gate fprintf(fo, "Default-Options: %s\n", hp->h_defopt), gotcha++; 678*0Sstevel@tonic-gate if ((w & GSUBJECT) && !(int)value("bsdcompat")) 679*0Sstevel@tonic-gate if (hp->h_subject != NOSTR && *hp->h_subject) 680*0Sstevel@tonic-gate fprintf(fo, "Subject: %s\n", hp->h_subject), gotcha++; 681*0Sstevel@tonic-gate else 682*0Sstevel@tonic-gate if (sflag && *sflag) 683*0Sstevel@tonic-gate fprintf(fo, "Subject: %s\n", sflag), gotcha++; 684*0Sstevel@tonic-gate if (hp->h_others != NOSTRPTR && (w & GOTHER)) { 685*0Sstevel@tonic-gate char **p; 686*0Sstevel@tonic-gate for (p = hp->h_others; *p; p++) 687*0Sstevel@tonic-gate fprintf(fo, "%s\n", *p); 688*0Sstevel@tonic-gate gotcha++; 689*0Sstevel@tonic-gate } 690*0Sstevel@tonic-gate #ifndef preSVr4 691*0Sstevel@tonic-gate if (w & GCLEN) 692*0Sstevel@tonic-gate fprintf(fo, "Content-Length: %ld\n", clen), gotcha++; 693*0Sstevel@tonic-gate #endif 694*0Sstevel@tonic-gate if (gotcha && (w & GNL)) 695*0Sstevel@tonic-gate putc('\n', fo); 696*0Sstevel@tonic-gate return(0); 697*0Sstevel@tonic-gate } 698*0Sstevel@tonic-gate 699*0Sstevel@tonic-gate /* 700*0Sstevel@tonic-gate * Format the given text to not exceed 78 characters. 701*0Sstevel@tonic-gate */ 702*0Sstevel@tonic-gate static void 703*0Sstevel@tonic-gate fmt(register char *str, register FILE *fo) 704*0Sstevel@tonic-gate { 705*0Sstevel@tonic-gate register int col = 4; 706*0Sstevel@tonic-gate char name[256]; 707*0Sstevel@tonic-gate int len; 708*0Sstevel@tonic-gate 709*0Sstevel@tonic-gate str = strcpy((char *)salloc(strlen(str)+1), str); 710*0Sstevel@tonic-gate while (str = yankword(str, name, sizeof (name), 1)) { 711*0Sstevel@tonic-gate len = strlen(name); 712*0Sstevel@tonic-gate if (col > 4) { 713*0Sstevel@tonic-gate if (col + len > 76) { 714*0Sstevel@tonic-gate fputs(",\n ", fo); 715*0Sstevel@tonic-gate col = 4; 716*0Sstevel@tonic-gate } else { 717*0Sstevel@tonic-gate fputs(", ", fo); 718*0Sstevel@tonic-gate col += 2; 719*0Sstevel@tonic-gate } 720*0Sstevel@tonic-gate } 721*0Sstevel@tonic-gate fputs(name, fo); 722*0Sstevel@tonic-gate col += len; 723*0Sstevel@tonic-gate } 724*0Sstevel@tonic-gate putc('\n', fo); 725*0Sstevel@tonic-gate } 726*0Sstevel@tonic-gate 727*0Sstevel@tonic-gate /* 728*0Sstevel@tonic-gate * Save the outgoing mail on the passed file. 729*0Sstevel@tonic-gate */ 730*0Sstevel@tonic-gate static int 731*0Sstevel@tonic-gate savemail(char name[], struct header *hp, FILE *fi) 732*0Sstevel@tonic-gate { 733*0Sstevel@tonic-gate register FILE *fo; 734*0Sstevel@tonic-gate time_t now; 735*0Sstevel@tonic-gate char *n; 736*0Sstevel@tonic-gate #ifdef preSVr4 737*0Sstevel@tonic-gate char line[BUFSIZ]; 738*0Sstevel@tonic-gate #else 739*0Sstevel@tonic-gate int c; 740*0Sstevel@tonic-gate #endif 741*0Sstevel@tonic-gate 742*0Sstevel@tonic-gate if (debug) 743*0Sstevel@tonic-gate fprintf(stderr, gettext("save in '%s'\n"), name); 744*0Sstevel@tonic-gate if ((fo = fopen(name, "a")) == NULL) { 745*0Sstevel@tonic-gate perror(name); 746*0Sstevel@tonic-gate return(-1); 747*0Sstevel@tonic-gate } 748*0Sstevel@tonic-gate time(&now); 749*0Sstevel@tonic-gate n = rflag; 750*0Sstevel@tonic-gate if (n == NOSTR) 751*0Sstevel@tonic-gate n = myname; 752*0Sstevel@tonic-gate fprintf(fo, "From %s %s", n, ctime(&now)); 753*0Sstevel@tonic-gate puthead(hp, fo, GMASK|GCLEN, fsize(fi) - textpos); 754*0Sstevel@tonic-gate fseek(fi, textpos, 0); 755*0Sstevel@tonic-gate #ifdef preSVr4 756*0Sstevel@tonic-gate while (fgets(line, sizeof line, fi)) { 757*0Sstevel@tonic-gate if (!strncmp(line, "From ", 5)) 758*0Sstevel@tonic-gate putc('>', fo); 759*0Sstevel@tonic-gate fputs(line, fo); 760*0Sstevel@tonic-gate } 761*0Sstevel@tonic-gate #else 762*0Sstevel@tonic-gate while ((c = getc(fi)) != EOF) 763*0Sstevel@tonic-gate putc(c, fo); 764*0Sstevel@tonic-gate #endif 765*0Sstevel@tonic-gate putc('\n', fo); 766*0Sstevel@tonic-gate fflush(fo); 767*0Sstevel@tonic-gate if (fferror(fo)) 768*0Sstevel@tonic-gate perror(name); 769*0Sstevel@tonic-gate fclose(fo); 770*0Sstevel@tonic-gate return(0); 771*0Sstevel@tonic-gate } 772