home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.sbin / sendmail / src / collect.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-20  |  10.0 KB  |  434 lines

  1. /*
  2.  * Copyright (c) 1983 Eric P. Allman
  3.  * Copyright (c) 1988 Regents of the University of California.
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  * 1. Redistributions of source code must retain the above copyright
  10.  *    notice, this list of conditions and the following disclaimer.
  11.  * 2. Redistributions in binary form must reproduce the above copyright
  12.  *    notice, this list of conditions and the following disclaimer in the
  13.  *    documentation and/or other materials provided with the distribution.
  14.  * 3. All advertising materials mentioning features or use of this software
  15.  *    must display the following acknowledgement:
  16.  *    This product includes software developed by the University of
  17.  *    California, Berkeley and its contributors.
  18.  * 4. Neither the name of the University nor the names of its contributors
  19.  *    may be used to endorse or promote products derived from this software
  20.  *    without specific prior written permission.
  21.  *
  22.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32.  * SUCH DAMAGE.
  33.  */
  34.  
  35. #ifndef lint
  36. static char sccsid[] = "@(#)collect.c    5.9 (Berkeley) 6/1/90";
  37. #endif /* not lint */
  38.  
  39. # include <errno.h>
  40. # include "sendmail.h"
  41.  
  42. /*
  43. **  COLLECT -- read & parse message header & make temp file.
  44. **
  45. **    Creates a temporary file name and copies the standard
  46. **    input to that file.  Leading UNIX-style "From" lines are
  47. **    stripped off (after important information is extracted).
  48. **
  49. **    Parameters:
  50. **        sayok -- if set, give an ARPANET style message
  51. **            to say we are ready to collect input.
  52. **
  53. **    Returns:
  54. **        none.
  55. **
  56. **    Side Effects:
  57. **        Temp file is created and filled.
  58. **        The from person may be set.
  59. */
  60.  
  61. collect(sayok)
  62.     bool sayok;
  63. {
  64.     register FILE *tf;
  65.     char buf[MAXFIELD], buf2[MAXFIELD];
  66.     register char *workbuf, *freebuf;
  67.     register int workbuflen;
  68.     extern char *hvalue();
  69.     extern bool isheader(), flusheol();
  70.  
  71.     /*
  72.     **  Create the temp file name and create the file.
  73.     */
  74.  
  75.     CurEnv->e_df = newstr(queuename(CurEnv, 'd'));
  76.     if ((tf = dfopen(CurEnv->e_df, "w")) == NULL)
  77.     {
  78.         syserr("Cannot create %s", CurEnv->e_df);
  79.         NoReturn = TRUE;
  80.         finis();
  81.     }
  82.     (void) chmod(CurEnv->e_df, FileMode);
  83.  
  84.     /*
  85.     **  Tell ARPANET to go ahead.
  86.     */
  87.  
  88.     if (sayok)
  89.         message("354", "Enter mail, end with \".\" on a line by itself");
  90.  
  91.     /*
  92.     **  Try to read a UNIX-style From line
  93.     */
  94.  
  95.     if (sfgets(buf, MAXFIELD, InChannel) == NULL)
  96.         goto readerr;
  97.     fixcrlf(buf, FALSE);
  98. # ifndef NOTUNIX
  99.     if (!SaveFrom && strncmp(buf, "From ", 5) == 0)
  100.     {
  101.         if (!flusheol(buf, InChannel))
  102.             goto readerr;
  103.         eatfrom(buf);
  104.         if (sfgets(buf, MAXFIELD, InChannel) == NULL)
  105.             goto readerr;
  106.         fixcrlf(buf, FALSE);
  107.     }
  108. # endif NOTUNIX
  109.  
  110.     /*
  111.     **  Copy InChannel to temp file & do message editing.
  112.     **    To keep certain mailers from getting confused,
  113.     **    and to keep the output clean, lines that look
  114.     **    like UNIX "From" lines are deleted in the header.
  115.     */
  116.  
  117.     workbuf = buf;        /* `workbuf' contains a header field */
  118.     freebuf = buf2;        /* `freebuf' can be used for read-ahead */
  119.     for (;;)
  120.     {
  121.         /* first, see if the header is over */
  122.         if (!isheader(workbuf))
  123.         {
  124.             fixcrlf(workbuf, TRUE);
  125.             break;
  126.         }
  127.  
  128.         /* if the line is too long, throw the rest away */
  129.         if (!flusheol(workbuf, InChannel))
  130.             goto readerr;
  131.  
  132.         /* it's okay to toss '\n' now (flusheol() needed it) */
  133.         fixcrlf(workbuf, TRUE);
  134.  
  135.         workbuflen = strlen(workbuf);
  136.  
  137.         /* get the rest of this field */
  138.         for (;;)
  139.         {
  140.             if (sfgets(freebuf, MAXFIELD, InChannel) == NULL)
  141.                 goto readerr;
  142.  
  143.             /* is this a continuation line? */
  144.             if (*freebuf != ' ' && *freebuf != '\t')
  145.                 break;
  146.  
  147.             if (!flusheol(freebuf, InChannel))
  148.                 goto readerr;
  149.  
  150.             /* yes; append line to `workbuf' if there's room */
  151.             if (workbuflen < MAXFIELD-3)
  152.             {
  153.                 register char *p = workbuf + workbuflen;
  154.                 register char *q = freebuf;
  155.  
  156.                 /* we have room for more of this field */
  157.                 fixcrlf(freebuf, TRUE);
  158.                 *p++ = '\n'; workbuflen++;
  159.                 while(*q != '\0' && workbuflen < MAXFIELD-1)
  160.                 {
  161.                     *p++ = *q++;
  162.                     workbuflen++;
  163.                 }
  164.                 *p = '\0';
  165.             }
  166.         }
  167.  
  168.         CurEnv->e_msgsize += workbuflen;
  169.  
  170.         /*
  171.         **  The working buffer now becomes the free buffer, since
  172.         **  the free buffer contains a new header field.
  173.         **
  174.         **  This is premature, since we still havent called
  175.         **  chompheader() to process the field we just created
  176.         **  (so the call to chompheader() will use `freebuf').
  177.         **  This convolution is necessary so that if we break out
  178.         **  of the loop due to H_EOH, `workbuf' will always be
  179.         **  the next unprocessed buffer.
  180.         */
  181.  
  182.         {
  183.             register char *tmp = workbuf;
  184.             workbuf = freebuf;
  185.             freebuf = tmp;
  186.         }
  187.  
  188.         /*
  189.         **  Snarf header away.
  190.         */
  191.  
  192.         if (bitset(H_EOH, chompheader(freebuf, FALSE)))
  193.             break;
  194.     }
  195.  
  196.     if (tTd(30, 1))
  197.         printf("EOH\n");
  198.  
  199.     if (*workbuf == '\0')
  200.     {
  201.         /* throw away a blank line */
  202.         if (sfgets(buf, MAXFIELD, InChannel) == NULL)
  203.             goto readerr;
  204.     }
  205.     else if (workbuf == buf2)    /* guarantee `buf' contains data */
  206.         (void) strcpy(buf, buf2);
  207.  
  208.     /*
  209.     **  Collect the body of the message.
  210.     */
  211.  
  212.     do
  213.     {
  214.         register char *bp = buf;
  215.  
  216.         fixcrlf(buf, TRUE);
  217.  
  218.         /* check for end-of-message */
  219.         if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0'))
  220.             break;
  221.  
  222.         /* check for transparent dot */
  223.         if (OpMode == MD_SMTP && !IgnrDot && bp[0] == '.' && bp[1] == '.')
  224.             bp++;
  225.  
  226.         /*
  227.         **  Figure message length, output the line to the temp
  228.         **  file, and insert a newline if missing.
  229.         */
  230.  
  231.         CurEnv->e_msgsize += strlen(bp) + 1;
  232.         fputs(bp, tf);
  233.         fputs("\n", tf);
  234.         if (ferror(tf))
  235.             tferror(tf);
  236.     } while (sfgets(buf, MAXFIELD, InChannel) != NULL);
  237.  
  238. readerr:
  239.     if (fflush(tf) != 0)
  240.         tferror(tf);
  241.     (void) fclose(tf);
  242.  
  243.     /* An EOF when running SMTP is an error */
  244.     if ((feof(InChannel) || ferror(InChannel)) && OpMode == MD_SMTP)
  245.     {
  246.         int usrerr(), syserr();
  247. # ifdef LOG
  248.         if (RealHostName != NULL && LogLevel > 0)
  249.             syslog(LOG_NOTICE,
  250.                 "collect: unexpected close on connection from %s: %m\n",
  251.                 CurEnv->e_from.q_paddr, RealHostName);
  252. # endif
  253.         (feof(InChannel) ? usrerr: syserr)
  254.             ("collect: unexpected close, from=%s", CurEnv->e_from.q_paddr);
  255.  
  256.         /* don't return an error indication */
  257.         CurEnv->e_to = NULL;
  258.         CurEnv->e_flags &= ~EF_FATALERRS;
  259.  
  260.         /* and don't try to deliver the partial message either */
  261.         finis();
  262.     }
  263.  
  264.     /*
  265.     **  Find out some information from the headers.
  266.     **    Examples are who is the from person & the date.
  267.     */
  268.  
  269.     eatheader(CurEnv);
  270.  
  271.     /*
  272.     **  Add an Apparently-To: line if we have no recipient lines.
  273.     */
  274.  
  275.     if (hvalue("to") == NULL && hvalue("cc") == NULL &&
  276.         hvalue("bcc") == NULL && hvalue("apparently-to") == NULL)
  277.     {
  278.         register ADDRESS *q;
  279.  
  280.         /* create an Apparently-To: field */
  281.         /*    that or reject the message.... */
  282.         for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next)
  283.         {
  284.             if (q->q_alias != NULL)
  285.                 continue;
  286.             if (tTd(30, 3))
  287.                 printf("Adding Apparently-To: %s\n", q->q_paddr);
  288.             addheader("apparently-to", q->q_paddr, CurEnv);
  289.         }
  290.     }
  291.  
  292.     if ((CurEnv->e_dfp = fopen(CurEnv->e_df, "r")) == NULL)
  293.         syserr("Cannot reopen %s", CurEnv->e_df);
  294. }
  295. /*
  296. **  FLUSHEOL -- if not at EOL, throw away rest of input line.
  297. **
  298. **    Parameters:
  299. **        buf -- last line read in (checked for '\n'),
  300. **        fp -- file to be read from.
  301. **
  302. **    Returns:
  303. **        FALSE on error from sfgets(), TRUE otherwise.
  304. **
  305. **    Side Effects:
  306. **        none.
  307. */
  308.  
  309. bool
  310. flusheol(buf, fp)
  311.     char *buf;
  312.     FILE *fp;
  313. {
  314.     char junkbuf[MAXLINE], *sfgets();
  315.     register char *p = buf;
  316.  
  317.     while (index(p, '\n') == NULL) {
  318.         if (sfgets(junkbuf,MAXLINE,fp) == NULL)
  319.             return(FALSE);
  320.         p = junkbuf;
  321.     }
  322.  
  323.     return(TRUE);
  324. }
  325. /*
  326. **  TFERROR -- signal error on writing the temporary file.
  327. **
  328. **    Parameters:
  329. **        tf -- the file pointer for the temporary file.
  330. **
  331. **    Returns:
  332. **        none.
  333. **
  334. **    Side Effects:
  335. **        Gives an error message.
  336. **        Arranges for following output to go elsewhere.
  337. */
  338.  
  339. tferror(tf)
  340.     FILE *tf;
  341. {
  342.     if (errno == ENOSPC)
  343.     {
  344.         (void) freopen(CurEnv->e_df, "w", tf);
  345.         fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
  346.         usrerr("452 Out of disk space for temp file");
  347.     }
  348.     else
  349.         syserr("collect: Cannot write %s", CurEnv->e_df);
  350.     (void) freopen("/dev/null", "w", tf);
  351. }
  352. /*
  353. **  EATFROM -- chew up a UNIX style from line and process
  354. **
  355. **    This does indeed make some assumptions about the format
  356. **    of UNIX messages.
  357. **
  358. **    Parameters:
  359. **        fm -- the from line.
  360. **
  361. **    Returns:
  362. **        none.
  363. **
  364. **    Side Effects:
  365. **        extracts what information it can from the header,
  366. **        such as the date.
  367. */
  368.  
  369. # ifndef NOTUNIX
  370.  
  371. char    *DowList[] =
  372. {
  373.     "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
  374. };
  375.  
  376. char    *MonthList[] =
  377. {
  378.     "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  379.     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
  380.     NULL
  381. };
  382.  
  383. eatfrom(fm)
  384.     char *fm;
  385. {
  386.     register char *p;
  387.     register char **dt;
  388.  
  389.     if (tTd(30, 2))
  390.         printf("eatfrom(%s)\n", fm);
  391.  
  392.     /* find the date part */
  393.     p = fm;
  394.     while (*p != '\0')
  395.     {
  396.         /* skip a word */
  397.         while (*p != '\0' && *p != ' ')
  398.             p++;
  399.         while (*p == ' ')
  400.             p++;
  401.         if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':')
  402.             continue;
  403.  
  404.         /* we have a possible date */
  405.         for (dt = DowList; *dt != NULL; dt++)
  406.             if (strncmp(*dt, p, 3) == 0)
  407.                 break;
  408.         if (*dt == NULL)
  409.             continue;
  410.  
  411.         for (dt = MonthList; *dt != NULL; dt++)
  412.             if (strncmp(*dt, &p[4], 3) == 0)
  413.                 break;
  414.         if (*dt != NULL)
  415.             break;
  416.     }
  417.  
  418.     if (*p != NULL)
  419.     {
  420.         char *q;
  421.         extern char *arpadate();
  422.  
  423.         /* we have found a date */
  424.         q = xalloc(25);
  425.         (void) strncpy(q, p, 25);
  426.         q[24] = '\0';
  427.         define('d', q, CurEnv);
  428.         q = arpadate(q);
  429.         define('a', newstr(q), CurEnv);
  430.     }
  431. }
  432.  
  433. # endif NOTUNIX
  434.