* Copyright (c) 1983 Eric P. Allman
* Copyright (c) 1988, 1993
* The Regents of the University of California. All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
static char sccsid
[] = "@(#)srvrsmtp.c 8.3 (Berkeley) 7/13/93 (with SMTP)";
static char sccsid
[] = "@(#)srvrsmtp.c 8.3 (Berkeley) 7/13/93 (without SMTP)";
** SMTP -- run the SMTP protocol.
** Reads commands from the input channel and processes
char *cmdname
; /* command name */
int cmdcode
; /* internal code, see below */
# define CMDERROR 0 /* bad command */
# define CMDMAIL 1 /* mail -- designate sender */
# define CMDRCPT 2 /* rcpt -- designate recipient */
# define CMDDATA 3 /* data -- send message text */
# define CMDRSET 4 /* rset -- reset state */
# define CMDVRFY 5 /* vrfy -- verify address */
# define CMDEXPN 6 /* expn -- expand address */
# define CMDNOOP 7 /* noop -- do nothing */
# define CMDQUIT 8 /* quit -- close connection and die */
# define CMDHELO 9 /* helo -- be polite */
# define CMDHELP 10 /* help -- give usage info */
# define CMDEHLO 11 /* ehlo -- extended helo (RFC 1425) */
/* non-standard commands */
# define CMDONEX 16 /* onex -- sending one transaction only */
# define CMDVERB 17 /* verb -- go into verbose mode */
/* debugging-only commands, only enabled if SMTPDEBUG is defined */
# define CMDDBGQSHOW 24 /* showq -- show send queue */
# define CMDDBGDEBUG 25 /* debug -- set debug mode */
static struct cmd CmdTab
[] =
* remaining commands are here only
* to trap and log attempts to use them
bool InChild
= FALSE
; /* true if running in a subprocess */
bool OneXact
= FALSE
; /* one xaction only this run */
#define EX_QUIT 22 /* special code for QUIT command */
bool gotmail
; /* mail command received */
bool gothello
; /* helo command received */
bool vrfy
; /* set if this is a vrfy command */
char *protocol
; /* sending protocol */
char *sendinghost
; /* sending hostname */
long msize
; /* approximate maximum message size */
int nrcpts
; /* number of RCPT commands */
extern ENVELOPE BlankEnvelope
;
if (fileno(OutChannel
) != fileno(stdout
))
/* arrange for debugging output to go to remote host */
(void) dup2(fileno(OutChannel
), fileno(stdout
));
CurHostName
= RealHostName
;
setproctitle("server %s startup", CurHostName
);
expand("\201e", inp
, &inp
[sizeof inp
], e
);
message("220 ESMTP spoken here");
sendinghost
= macvalue('s', e
);
/* arrange for backout */
if (setjmp(TopFrame
) > 0 && InChild
)
e
->e_flags
&= ~EF_VRFYONLY
;
/* read the input line */
SmtpPhase
= "server cmd read";
setproctitle("server %s cmd read", CurHostName
);
p
= sfgets(inp
, sizeof inp
, InChannel
, TimeOuts
.to_nextcommand
,
/* end of file, just die */
message("421 %s Lost input channel from %s",
MyHostName
, CurHostName
);
syslog(LOG_NOTICE
, "lost input channel from %s",
/* clean up end of line */
/* echo command to transcript */
fprintf(e
->e_xfp
, "<<< %s\n", inp
);
setproctitle("%s: %s", CurHostName
, inp
);
setproctitle("%s %s: %s", e
->e_id
, CurHostName
, inp
);
for (p
= inp
; isascii(*p
) && isspace(*p
); p
++)
!(isascii(*p
) && isspace(*p
)) &&
cmd
< &cmdbuf
[sizeof cmdbuf
- 2])
/* throw away leading whitespace */
while (isascii(*p
) && isspace(*p
))
for (c
= CmdTab
; c
->cmdname
!= NULL
; c
++)
if (!strcasecmp(c
->cmdname
, cmdbuf
))
case CMDHELO
: /* hello -- introduce yourself */
case CMDEHLO
: /* extended hello */
if (c
->cmdcode
== CMDEHLO
)
SmtpPhase
= "server EHLO";
SmtpPhase
= "server HELO";
if (strcasecmp(p
, RealHostName
) != 0)
auth_warning(e
, "Host %s claimed to be %s",
if (c
->cmdcode
!= CMDEHLO
)
/* print old message and be done with it */
message("250 %s Hello %s, pleased to meet you",
/* print extended message and brag */
message("250-%s Hello %s, pleased to meet you",
if (!bitset(PRIV_NOEXPN
, PrivacyFlags
))
message("250-SIZE %ld", MaxMessageSize
);
case CMDMAIL
: /* mail -- designate sender */
SmtpPhase
= "server MAIL";
/* check for validity of this command */
/* set sending host to our known value */
sendinghost
= RealHostName
;
if (bitset(PRIV_NEEDMAILHELO
, PrivacyFlags
))
message("503 Polite people say HELO first");
message("503 Sender already specified");
syserr("503 Nested MAIL command: MAIL %s", p
);
/* fork a subprocess to process this command */
if (runinchild("SMTP-MAIL", e
) > 0)
"Host %s didn't use HELO protocol",
define('r', protocol
, e
);
define('s', sendinghost
, e
);
setproctitle("%s %s: %s", e
->e_id
, CurHostName
, inp
);
/* child -- go do the processing */
if (setjmp(TopFrame
) > 0)
/* this failed -- undo work */
e
->e_flags
&= ~EF_FATALERRS
;
/* must parse sender first */
setsender(p
, e
, &delimptr
, FALSE
);
if (p
!= NULL
&& *p
!= '\0')
/* now parse ESMTP arguments */
for (; p
!= NULL
&& *p
!= '\0'; p
++)
/* locate the beginning of the keyword */
while (isascii(*p
) && isspace(*p
))
/* skip to the value portion */
while (isascii(*p
) && isalnum(*p
) || *p
== '-')
/* skip to the end of the value */
while (*p
!= '\0' && *p
!= ' ' &&
!(isascii(*p
) && iscntrl(*p
)) &&
printf("MAIL: got arg %s=%s\n", kp
,
vp
== NULL
? "<null>" : vp
);
if (strcasecmp(kp
, "size") == 0)
usrerr("501 SIZE requires a value");
else if (strcasecmp(kp
, "body") == 0)
usrerr("501 BODY requires a value");
if (strcasecmp(vp
, "8bitmime") == 0)
e
->e_bodytype
= "8BITMIME";
else if (strcasecmp(vp
, "7bit") == 0)
usrerr("501 Unknown BODY type %s",
usrerr("501 %s parameter unrecognized", kp
);
if (MaxMessageSize
> 0 && msize
> MaxMessageSize
)
usrerr("552 Message size exceeds fixed maximum message size (%ld)",
message("452 Insufficient disk space; try again later");
message("250 Sender ok");
case CMDRCPT
: /* rcpt -- designate recipient */
usrerr("503 Need MAIL before RCPT");
SmtpPhase
= "server RCPT";
if (setjmp(TopFrame
) > 0)
e
->e_flags
&= ~EF_FATALERRS
;
if (e
->e_sendmode
!= SM_DELIVER
)
e
->e_flags
|= EF_VRFYONLY
;
a
= parseaddr(p
, (ADDRESS
*) NULL
, 1, ' ', NULL
, e
);
a
= recipient(a
, &e
->e_sendqueue
, e
);
/* no errors during parsing, but might be a duplicate */
if (!bitset(QBADADDR
, a
->q_flags
))
message("250 Recipient ok");
/* punt -- should keep message in ADDRESS.... */
message("550 Addressee unknown");
case CMDDATA
: /* data -- text of mail */
SmtpPhase
= "server DATA";
message("503 Need MAIL command");
else if (e
->e_nrcpts
<= 0)
message("503 Need RCPT (recipient)");
/* check to see if we need to re-expand aliases */
/* also reset QBADADDR on already-diagnosted addrs */
for (a
= e
->e_sendqueue
; a
!= NULL
; a
= a
->q_next
)
if (bitset(QVERIFIED
, a
->q_flags
))
/* need to re-expand aliases */
if (bitset(QBADADDR
, a
->q_flags
))
/* make this "go away" */
/* collect the text of the message */
collect(TRUE
, doublequeue
, e
);
e
->e_flags
&= ~EF_FATALERRS
;
** Arrange to send to everyone.
** If sending to multiple people, mail back
** errors rather than reporting directly.
** In any case, don't mail back errors for
** anything that has happened up to
** now (the other end will do this).
** Truncate our transcript -- the mail has gotten
** to us successfully, and if we have
** to mail this back, it will be easier
** Then send to everyone.
** Finally give a reply code. If an error has
** already been given, don't mail a
** We goose error returns by clearing error bit.
if (nrcpts
!= 1 && !doublequeue
)
e
->e_errormode
= EM_MAIL
;
e
->e_xfp
= freopen(queuename(e
, 'x'), "w", e
->e_xfp
);
/* send to all recipients */
sendall(e
, doublequeue
? SM_QUEUE
: SM_DEFAULT
);
markstats(e
, (ADDRESS
*) NULL
);
/* issue success if appropriate and reset */
if (Errors
== 0 || HoldErrs
)
message("250 %s Message accepted for delivery", id
);
if (bitset(EF_FATALERRS
, e
->e_flags
) && !HoldErrs
)
/* avoid sending back an extra message */
e
->e_flags
&= ~EF_FATALERRS
;
e
->e_flags
|= EF_CLRQUEUE
;
/* from now on, we have to operate silently */
e
->e_errormode
= EM_MAIL
;
/* if we just queued, poke it */
if (doublequeue
&& e
->e_sendmode
!= SM_QUEUE
)
dowork(id
, TRUE
, TRUE
, e
);
/* if in a child, pop back to our parent */
CurEnv
= e
= newenvelope(e
, CurEnv
);
e
->e_flags
= BlankEnvelope
.e_flags
;
case CMDRSET
: /* rset -- reset state */
message("250 Reset state");
CurEnv
= e
= newenvelope(e
, CurEnv
);
case CMDVRFY
: /* vrfy -- verify address */
case CMDEXPN
: /* expn -- expand address */
vrfy
= c
->cmdcode
== CMDVRFY
;
if (bitset(vrfy
? PRIV_NOVRFY
: PRIV_NOEXPN
,
message("252 Who's to say?");
message("502 That's none of your business");
bitset(vrfy
? PRIV_NEEDVRFYHELO
: PRIV_NEEDEXPNHELO
,
message("503 I demand that you introduce yourself first");
if (runinchild(vrfy
? "SMTP-VRFY" : "SMTP-EXPN", e
) > 0)
syslog(LOG_INFO
, "%s: %s", CurHostName
, inp
);
e
->e_flags
|= EF_VRFYONLY
;
while (*p
!= '\0' && isascii(*p
) && isspace(*p
))
message("501 Argument required");
(void) sendtolist(p
, (ADDRESS
*) NULL
,
message("554 Nothing to %s", vrfy
? "VRFY" : "EXPN");
while (vrfyqueue
!= NULL
)
register ADDRESS
*a
= vrfyqueue
->q_next
;
while (a
!= NULL
&& bitset(QDONTSEND
|QBADADDR
, a
->q_flags
))
if (!bitset(QDONTSEND
|QBADADDR
, vrfyqueue
->q_flags
))
printvrfyaddr(vrfyqueue
, a
== NULL
);
message("554 Self destructive alias loop");
case CMDHELP
: /* help -- give user info */
case CMDNOOP
: /* noop -- do nothing */
case CMDQUIT
: /* quit -- leave mail */
message("221 %s closing connection", MyHostName
);
/* avoid future 050 messages */
case CMDVERB
: /* set verbose mode */
if (bitset(PRIV_NOEXPN
, PrivacyFlags
))
/* this would give out the same info */
message("502 Verbose unavailable");
e
->e_sendmode
= SM_DELIVER
;
message("250 Verbose mode");
case CMDONEX
: /* doing one transaction only */
message("250 Only one transaction");
case CMDDBGQSHOW
: /* show queues */
printaddr(e
->e_sendqueue
, TRUE
);
case CMDDBGDEBUG
: /* set debug mode */
tTsetup(tTdvect
, sizeof tTdvect
, "0-99.1");
message("200 Debug set");
# else /* not SMTPDEBUG */
case CMDDBGQSHOW
: /* show queues */
case CMDDBGDEBUG
: /* set debug mode */
"\"%s\" command from %s (%s)",
c
->cmdname
, RealHostName
,
anynet_ntoa(&RealHostAddr
));
case CMDERROR
: /* unknown command */
message("500 Command unrecognized");
syserr("500 smtp: unknown code %d", c
->cmdcode
);
** SKIPWORD -- skip a fixed word.
** p -- place to start looking.
** clobbers the p data area.
/* find beginning of word */
while (isascii(*p
) && isspace(*p
))
while (*p
!= '\0' && *p
!= ':' && !(isascii(*p
) && isspace(*p
)))
while (isascii(*p
) && isspace(*p
))
message("501 Syntax error in parameters");
while (isascii(*p
) && isspace(*p
))
/* see if the input word matches desired word */
** PRINTVRFYADDR -- print an entry in the verify queue
** a -- the address to print
** last -- set if this is the last one.
** Prints the appropriate 250 codes.
fmtbuf
[3] = last
? ' ' : '-';
if (a
->q_fullname
== NULL
)
if (strchr(a
->q_user
, '@') == NULL
)
strcpy(&fmtbuf
[4], "<%s@%s>");
strcpy(&fmtbuf
[4], "<%s>");
message(fmtbuf
, a
->q_user
, MyHostName
);
if (strchr(a
->q_user
, '@') == NULL
)
strcpy(&fmtbuf
[4], "%s <%s@%s>");
strcpy(&fmtbuf
[4], "%s <%s>");
message(fmtbuf
, a
->q_fullname
, a
->q_user
, MyHostName
);
** HELP -- implement the HELP command.
** topic -- the topic we want help for.
** outputs the help file to message output.
if (HelpFile
== NULL
|| (hf
= fopen(HelpFile
, "r")) == NULL
)
message("502 HELP not implemented");
if (topic
== NULL
|| *topic
== '\0')
while (fgets(buf
, sizeof buf
, hf
) != NULL
)
if (strncmp(buf
, topic
, len
) == 0)
message("504 HELP topic unknown");
message("214 End of HELP info");
** RUNINCHILD -- return twice -- once in the child, then in the parent again
** label -- a string used in error messages
syserr("%s: cannot fork", label
);
/* parent -- wait for child to complete */
setproctitle("server %s child wait", CurHostName
);
syserr("%s: lost child", label
);
/* if we exited on a QUIT command, complete the process */
if (st
== (EX_QUIT
<< 8))
/* open alias database */