10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
22*373Sceastha /*
23*373Sceastha * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24*373Sceastha * Use is subject to license terms.
25*373Sceastha */
26*373Sceastha
270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
300Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
310Sstevel@tonic-gate
320Sstevel@tonic-gate #include "mail.h"
33*373Sceastha
340Sstevel@tonic-gate /*
350Sstevel@tonic-gate NAME
360Sstevel@tonic-gate copylet - copy a given letter to a file pointer
370Sstevel@tonic-gate
380Sstevel@tonic-gate SYNOPSIS
390Sstevel@tonic-gate int copylet(int letnum, FILE *f, int type)
400Sstevel@tonic-gate
410Sstevel@tonic-gate DESCRIPTION
420Sstevel@tonic-gate Copylet() will copy the letter "letnum" to the
430Sstevel@tonic-gate given file pointer.
440Sstevel@tonic-gate
450Sstevel@tonic-gate letnum -> index into: letter table
460Sstevel@tonic-gate f -> file pointer to copy file to
470Sstevel@tonic-gate type -> copy type
480Sstevel@tonic-gate
490Sstevel@tonic-gate Returns TRUE on a completely successful copy.
500Sstevel@tonic-gate */
510Sstevel@tonic-gate
52*373Sceastha int
copylet(int letnum,FILE * f,int type)53*373Sceastha copylet(int letnum, FILE *f, int type)
540Sstevel@tonic-gate {
550Sstevel@tonic-gate int pos = ftell(f);
560Sstevel@tonic-gate int rc = xxxcopylet(letnum, f, type);
570Sstevel@tonic-gate
580Sstevel@tonic-gate if (fflush(f) != 0)
590Sstevel@tonic-gate rc = FALSE;
600Sstevel@tonic-gate
610Sstevel@tonic-gate /*
620Sstevel@tonic-gate * On error, truncate the file to its original position so that a
630Sstevel@tonic-gate * partial message is not left in the mailbox.
640Sstevel@tonic-gate */
650Sstevel@tonic-gate if (rc == FALSE)
660Sstevel@tonic-gate ftruncate(fileno(f), pos);
670Sstevel@tonic-gate
680Sstevel@tonic-gate return(rc);
690Sstevel@tonic-gate }
700Sstevel@tonic-gate
71*373Sceastha int
xxxcopylet(int letnum,FILE * f,int type)72*373Sceastha xxxcopylet(int letnum, FILE *f, int type)
730Sstevel@tonic-gate {
740Sstevel@tonic-gate static char pn[] = "copylet";
750Sstevel@tonic-gate char buf[LSIZE], lastc;
760Sstevel@tonic-gate char wbuf[LSIZE];
770Sstevel@tonic-gate int n;
780Sstevel@tonic-gate long i, k;
790Sstevel@tonic-gate int num;
800Sstevel@tonic-gate int rtrncont = 1; /* True: nondelivery&content included, or regular mail */
810Sstevel@tonic-gate int suppress = FALSE;
820Sstevel@tonic-gate int sav_suppress = FALSE; /* Did we suppress previous hdr line? */
830Sstevel@tonic-gate int print_from_struct = FALSE; /* print from hdrlines struct */
840Sstevel@tonic-gate /* rather than fgets() buffer */
850Sstevel@tonic-gate int pushrest = FALSE;
860Sstevel@tonic-gate int ctf = FALSE;
870Sstevel@tonic-gate int didafflines = FALSE; /* Did we already put out any */
880Sstevel@tonic-gate /* H_AFWDFROM lines? */
890Sstevel@tonic-gate int didrcvlines = FALSE; /* Did we already put out any */
900Sstevel@tonic-gate /* H_RECEIVED lines? */
910Sstevel@tonic-gate long clen = -1L;
920Sstevel@tonic-gate int htype; /* header type */
930Sstevel@tonic-gate int sav_htype; /* Header type of last non-H_CONT header line */
940Sstevel@tonic-gate struct hdrs *hptr;
950Sstevel@tonic-gate
960Sstevel@tonic-gate if (!sending) {
970Sstevel@tonic-gate /* Clear out any saved header info from previous message */
980Sstevel@tonic-gate clr_hinfo();
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate fseek(tmpf, let[letnum].adr, 0);
1020Sstevel@tonic-gate /* Get size of message as stored into tempfile by copymt() */
1030Sstevel@tonic-gate k = let[letnum+1].adr - let[letnum].adr;
1040Sstevel@tonic-gate Dout(pn, 1, "(letnum = %d, type = %d), k = %ld\n", letnum, type, k);
1050Sstevel@tonic-gate while (k>0) { /* process header */
1060Sstevel@tonic-gate num = ((k < sizeof(buf)) ? k+1 : sizeof(buf));
1070Sstevel@tonic-gate if (fgets (buf, num, tmpf) == NULL) {
1080Sstevel@tonic-gate return (FALSE);
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate if ((n = strlen (buf)) == 0) {
1110Sstevel@tonic-gate k = 0;
1120Sstevel@tonic-gate break;
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate k -= n;
1150Sstevel@tonic-gate lastc = buf[n-1];
1160Sstevel@tonic-gate if (pushrest) {
1170Sstevel@tonic-gate pushrest = (lastc != '\n');
1180Sstevel@tonic-gate continue;
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate htype = isheader (buf, &ctf);
1210Sstevel@tonic-gate Dout(pn, 5, "loop 1: buf = %s, htype= %d/%s\n", buf, htype, header[htype].tag);
1220Sstevel@tonic-gate if (htype == H_CLEN) {
1230Sstevel@tonic-gate if (!sending) {
1240Sstevel@tonic-gate savehdrs(buf,htype);
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate if ((hptr = hdrlines[H_CLEN].head) !=
1270Sstevel@tonic-gate (struct hdrs *)NULL) {
1280Sstevel@tonic-gate clen = atol (hptr->value);
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate if (type == ZAP) {
1320Sstevel@tonic-gate if (htype != FALSE) {
1330Sstevel@tonic-gate pushrest = (lastc != '\n');
1340Sstevel@tonic-gate continue;
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate /* end of header. Print non-blank line and bail. */
1370Sstevel@tonic-gate Dout(pn, 5, "ZAP end header; n=%d, buf[0] = %d\n", n, buf[0]);
1380Sstevel@tonic-gate if (buf[0] != '\n') {
1390Sstevel@tonic-gate if (fwrite(buf,1,n,f) != n) {
1400Sstevel@tonic-gate sav_errno = errno;
1410Sstevel@tonic-gate return(FALSE);
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate } else {
1440Sstevel@tonic-gate n = 0;
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate break;
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate /* Copy From line appropriately */
1490Sstevel@tonic-gate if (fwrite(buf,1,n-1,f) != n-1) {
1500Sstevel@tonic-gate sav_errno = errno;
1510Sstevel@tonic-gate return(FALSE);
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate if (lastc != '\n') {
1540Sstevel@tonic-gate if (fwrite(&lastc,1,1,f) != 1) {
1550Sstevel@tonic-gate sav_errno = errno;
1560Sstevel@tonic-gate return(FALSE);
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate continue;
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate switch(type) {
1610Sstevel@tonic-gate case REMOTE:
1620Sstevel@tonic-gate if (fprintf(f, rmtmsg, thissys) < 0)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate sav_errno = errno;
1650Sstevel@tonic-gate return(FALSE);
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate break;
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate case TTY:
1710Sstevel@tonic-gate case ORDINARY:
1720Sstevel@tonic-gate default:
1730Sstevel@tonic-gate if (fprintf(f, "\n") < 0)
1740Sstevel@tonic-gate {
1750Sstevel@tonic-gate sav_errno = errno;
1760Sstevel@tonic-gate return(FALSE);
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate break;
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate if ((error > 0) && (dflag == 1)) {
1810Sstevel@tonic-gate Dout(pn, 3, "before gendeliv(), uval = '%s'\n", uval);
1820Sstevel@tonic-gate gendeliv(f, dflag, uval);
1830Sstevel@tonic-gate if (!(ckdlivopts(H_TCOPY, (int*)0) & RETURN)) {
1840Sstevel@tonic-gate rtrncont = 0;
1850Sstevel@tonic-gate } else {
1860Sstevel@tonic-gate /* Account for content-type info */
1870Sstevel@tonic-gate /* of returned msg */
1880Sstevel@tonic-gate if (fprintf(f, "%s %s\n", header[H_CTYPE].tag,
1890Sstevel@tonic-gate (let[letnum].text == TRUE ? "text/plain" : "application/octet-stream")) < 0)
1900Sstevel@tonic-gate {
1910Sstevel@tonic-gate sav_errno = errno;
1920Sstevel@tonic-gate return(FALSE);
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate /* Compute Content-Length of what's being */
1960Sstevel@tonic-gate /* returned... */
1970Sstevel@tonic-gate i = k;
1980Sstevel@tonic-gate /* Account for H_AFWDFROM, H_AFWDCNT, */
1990Sstevel@tonic-gate /* H_TCOPY, or H_RECEIVED lines which may */
2000Sstevel@tonic-gate /* be added later */
2010Sstevel@tonic-gate if (affcnt > 0) {
2020Sstevel@tonic-gate sprintf(wbuf, "%d", affcnt);
2030Sstevel@tonic-gate i += (affbytecnt
2040Sstevel@tonic-gate + strlen(header[H_AFWDCNT].tag)
2050Sstevel@tonic-gate + strlen(wbuf) + 2);
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate if (orig_tcopy) {
2080Sstevel@tonic-gate if ((hptr = hdrlines[H_TCOPY].head) !=
2090Sstevel@tonic-gate (struct hdrs *)NULL) {
2100Sstevel@tonic-gate i +=
2110Sstevel@tonic-gate strlen(hdrlines[H_TCOPY].head->value);
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate if ((hptr = hdrlines[H_RECEIVED].head) !=
2150Sstevel@tonic-gate (struct hdrs *)NULL) {
2160Sstevel@tonic-gate i += rcvbytecnt;
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate /* Add in strlen of MIME-Version:, */
2190Sstevel@tonic-gate /* Content-Length: and Content-Type: */
2200Sstevel@tonic-gate /* values for msg being returned... */
2210Sstevel@tonic-gate if ((hptr = hdrlines[H_MIMEVERS].head) !=
2220Sstevel@tonic-gate (struct hdrs *)NULL) {
2230Sstevel@tonic-gate i += strlen(hdrlines[H_MIMEVERS].head->value);
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate if ((hptr = hdrlines[H_CTYPE].head) !=
2260Sstevel@tonic-gate (struct hdrs *)NULL) {
2270Sstevel@tonic-gate i += strlen(hdrlines[H_CTYPE].head->value);
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate if ((hptr = hdrlines[H_CLEN].head) !=
2300Sstevel@tonic-gate (struct hdrs *)NULL) {
2310Sstevel@tonic-gate i += strlen(hdrlines[H_CLEN].head->value);
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate if (fprintf(f, "%s %ld\n", header[H_CLEN].tag, i) < 0)
2340Sstevel@tonic-gate {
2350Sstevel@tonic-gate sav_errno = errno;
2360Sstevel@tonic-gate return(FALSE);
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate if (fprintf(f, "\n") < 0)
2400Sstevel@tonic-gate {
2410Sstevel@tonic-gate sav_errno = errno;
2420Sstevel@tonic-gate return(FALSE);
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate if (fflush(f))
2460Sstevel@tonic-gate {
2470Sstevel@tonic-gate sav_errno = errno;
2480Sstevel@tonic-gate return(FALSE);
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate
2510Sstevel@tonic-gate break;
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate /* if not ZAP, copy balance of header */
2540Sstevel@tonic-gate n = 0;
2550Sstevel@tonic-gate if ((type != ZAP) && rtrncont)
2560Sstevel@tonic-gate while (k>0 || n>0) {
2570Sstevel@tonic-gate if ((n > 0) && !suppress) {
2580Sstevel@tonic-gate if (print_from_struct == TRUE) {
2590Sstevel@tonic-gate if (printhdr (type, htype, hptr, f) < 0) {
2600Sstevel@tonic-gate return (FALSE);
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate } else {
2630Sstevel@tonic-gate if (sel_disp(type, htype, buf) >= 0) {
2640Sstevel@tonic-gate if (fwrite(buf,1,n,f) != n) {
2650Sstevel@tonic-gate sav_errno = errno;
2660Sstevel@tonic-gate return(FALSE);
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate }
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate if (htype == H_DATE) {
2710Sstevel@tonic-gate dumprcv(type, htype,&didrcvlines,&suppress,f);
2720Sstevel@tonic-gate dumpaff(type, htype,&didafflines,&suppress,f);
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate if (k <= 0) {
2760Sstevel@tonic-gate /* Can only get here if k=0 && n>0, which occurs */
2770Sstevel@tonic-gate /* in a message with header lines but no content. */
2780Sstevel@tonic-gate /* If we haven't already done it, force out any */
2790Sstevel@tonic-gate /* H_AFWDFROM or H_RECEIVED lines */
2800Sstevel@tonic-gate dumprcv(type, -1,&didrcvlines,&suppress,f);
2810Sstevel@tonic-gate dumpaff(type, -1,&didafflines,&suppress,f);
2820Sstevel@tonic-gate break;
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate num = ((k < sizeof(buf)) ? k+1 : sizeof(buf));
2850Sstevel@tonic-gate if (fgets (buf, num, tmpf) == NULL) {
2860Sstevel@tonic-gate return (FALSE);
2870Sstevel@tonic-gate }
2880Sstevel@tonic-gate n = strlen (buf);
2890Sstevel@tonic-gate k -= n;
2900Sstevel@tonic-gate lastc = buf[n-1];
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate if (pushrest) {
2930Sstevel@tonic-gate pushrest = (lastc != '\n');
2940Sstevel@tonic-gate continue;
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate sav_suppress = suppress;
2970Sstevel@tonic-gate suppress = FALSE;
2980Sstevel@tonic-gate print_from_struct = FALSE;
2990Sstevel@tonic-gate sav_htype = htype;
3000Sstevel@tonic-gate htype = isheader (buf, &ctf);
3010Sstevel@tonic-gate Dout(pn, 5, "loop 2: buf = %s, htype= %d/%s\n", buf, htype, header[htype].tag);
3020Sstevel@tonic-gate /* The following order is defined in the MTA documents. */
3030Sstevel@tonic-gate switch (htype) {
3040Sstevel@tonic-gate case H_CONT:
3050Sstevel@tonic-gate if (sending) {
3060Sstevel@tonic-gate suppress = sav_suppress;
3070Sstevel@tonic-gate }
3080Sstevel@tonic-gate continue;
3090Sstevel@tonic-gate case H_TCOPY:
3100Sstevel@tonic-gate case H_MIMEVERS:
3110Sstevel@tonic-gate case H_CTYPE:
3120Sstevel@tonic-gate case H_CLEN:
3130Sstevel@tonic-gate if (!sending) {
3140Sstevel@tonic-gate savehdrs(buf,htype);
3150Sstevel@tonic-gate }
3160Sstevel@tonic-gate hptr = hdrlines[htype].head;
3170Sstevel@tonic-gate if (htype == H_CLEN) {
3180Sstevel@tonic-gate clen = atol (hptr->value);
3190Sstevel@tonic-gate }
3200Sstevel@tonic-gate /*
3210Sstevel@tonic-gate * Use values saved in hdrlines[] structure
3220Sstevel@tonic-gate * rather than what was read from tmp file.
3230Sstevel@tonic-gate */
3240Sstevel@tonic-gate print_from_struct = TRUE;
3250Sstevel@tonic-gate /* FALLTHROUGH */
3260Sstevel@tonic-gate case H_EOH:
3270Sstevel@tonic-gate case H_AFWDFROM:
3280Sstevel@tonic-gate case H_AFWDCNT:
3290Sstevel@tonic-gate case H_RECEIVED:
3300Sstevel@tonic-gate dumprcv(type, htype,&didrcvlines,&suppress,f);
3310Sstevel@tonic-gate dumpaff(type, htype,&didafflines,&suppress,f);
3320Sstevel@tonic-gate continue; /* next header line */
3330Sstevel@tonic-gate default:
3340Sstevel@tonic-gate pushrest = (lastc != '\n');
3350Sstevel@tonic-gate continue; /* next header line */
3360Sstevel@tonic-gate case FALSE: /* end of header */
3370Sstevel@tonic-gate break;
3380Sstevel@tonic-gate }
3390Sstevel@tonic-gate
3400Sstevel@tonic-gate /* Found the blank line after the headers. */
3410Sstevel@tonic-gate if (n > 0) {
3420Sstevel@tonic-gate if (fwrite(buf,1,n,f) != n) {
3430Sstevel@tonic-gate sav_errno = errno;
3440Sstevel@tonic-gate return(FALSE);
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate }
3470Sstevel@tonic-gate
3480Sstevel@tonic-gate Dout(pn, 3,", let[%d].text = %s\n",
3490Sstevel@tonic-gate letnum, (let[letnum].text ? "TRUE" : "FALSE"));
3500Sstevel@tonic-gate
3510Sstevel@tonic-gate if ((type == TTY) && (let[letnum].text == FALSE) && !pflg) {
3520Sstevel@tonic-gate if (fprintf (f, "\n%s\n", binmsg) < 0)
3530Sstevel@tonic-gate {
3540Sstevel@tonic-gate sav_errno = errno;
3550Sstevel@tonic-gate return(FALSE);
3560Sstevel@tonic-gate }
3570Sstevel@tonic-gate return (TRUE);
3580Sstevel@tonic-gate }
3590Sstevel@tonic-gate
3600Sstevel@tonic-gate if (n == 1 && buf[0] == '\n') {
3610Sstevel@tonic-gate n = 0;
3620Sstevel@tonic-gate }
3630Sstevel@tonic-gate break;
3640Sstevel@tonic-gate }
3650Sstevel@tonic-gate
3660Sstevel@tonic-gate Dout(pn, 1, "header processed, clen/k/n = %ld/%ld/%d\n", clen, k, n);
3670Sstevel@tonic-gate
3680Sstevel@tonic-gate if (clen >= 0) {
3690Sstevel@tonic-gate if (((clen - n) == k) || ((clen - n) == (k - 1))) {
3700Sstevel@tonic-gate k = clen - n;
3710Sstevel@tonic-gate } else {
3720Sstevel@tonic-gate /* probable content-length mismatch. show it ALL! */
3730Sstevel@tonic-gate Dout(pn, 1, "clen conflict. using k = %ld\n", k);
3740Sstevel@tonic-gate }
3750Sstevel@tonic-gate }
3760Sstevel@tonic-gate
3770Sstevel@tonic-gate /* copy balance of message */
3780Sstevel@tonic-gate if (rtrncont)
3790Sstevel@tonic-gate while (k > 0) {
3800Sstevel@tonic-gate num = ((k < sizeof(buf)) ? k : sizeof(buf));
3810Sstevel@tonic-gate if ((n = fread (buf, 1, num, tmpf)) <= 0) {
3820Sstevel@tonic-gate Dout(pn, 1, "content-length mismatch. return(FALSE)\n");
3830Sstevel@tonic-gate return(FALSE);
3840Sstevel@tonic-gate }
3850Sstevel@tonic-gate k -= n;
3860Sstevel@tonic-gate if (fwrite(buf,1,n,f) != n) {
3870Sstevel@tonic-gate sav_errno = errno;
3880Sstevel@tonic-gate return(FALSE);
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate }
3910Sstevel@tonic-gate
3920Sstevel@tonic-gate Dout(pn, 3, "body processed, k=%ld\n", k);
3930Sstevel@tonic-gate
3940Sstevel@tonic-gate if (rtrncont && type != ZAP && type != REMOTE) {
3950Sstevel@tonic-gate if (fwrite("\n",1,1,f) != 1) {
3960Sstevel@tonic-gate sav_errno = errno;
3970Sstevel@tonic-gate return(FALSE);
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate }
4000Sstevel@tonic-gate
4010Sstevel@tonic-gate return(TRUE);
4020Sstevel@tonic-gate }
403