* Copyright (c) 1983 Eric P. Allman
* Copyright (c) 1988, 1993
* The Regents of the University of California. All rights reserved.
* %sccs.include.redist.c%
static char sccsid
[] = "@(#)srvrsmtp.c 8.62 (Berkeley) %G% (with SMTP)";
static char sccsid
[] = "@(#)srvrsmtp.c 8.62 (Berkeley) %G% (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 CMDHOPS 4 /* hops -- specify hop count */
# 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 */
/* use this to catch and log "door handle" attempts on your system */
# define CMDLOGBOGUS 23 /* bogus command that should be logged */
/* 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 OneXact
= FALSE
; /* one xaction only this run */
char *CurSmtpClient
; /* who's at the other end of channel */
extern char RealUserName
[];
#define MAXBADCOMMANDS 25 /* maximum number of bad commands */
extern ADDRESS
*sendto();
if (fileno(OutChannel
) != fileno(stdout
))
/* arrange for debugging output to go to remote host */
(void) dup2(fileno(OutChannel
), fileno(stdout
));
peerhostname
= RealHostName
;
if (peerhostname
== NULL
)
peerhostname
= "localhost";
CurHostName
= peerhostname
;
CurSmtpClient
= macvalue('_', e
);
if (CurSmtpClient
== NULL
)
CurSmtpClient
= CurHostName
;
setproctitle("server %s startup", CurSmtpClient
);
expand("\201e", inp
, sizeof inp
, e
);
message("220 ESMTP spoken here");
sendinghost
= macvalue('s', e
);
/* arrange for backout */
if (setjmp(TopFrame
) > 0)
/* if() nesting is necessary for Cray UNICOS */
e
->e_flags
&= ~(EF_VRFYONLY
|EF_GLOBALERRS
);
/* 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
, CurSmtpClient
);
if (LogLevel
> (gotmail
? 1 : 19))
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: %.80s", CurSmtpClient
, inp
);
setproctitle("%s %s: %.80s", e
->e_id
, CurSmtpClient
, 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";
/* check for valid domain name (re 1123 5.2.5) */
message("501 %s requires domain address",
for (q
= p
; *q
!= '\0'; q
++)
if (strchr("[].-_#", *q
) == NULL
)
message("501 Invalid domain name");
message("250", "%s Hello %s, pleased to meet you", HostName
, p
);
case CMDMAIL
: /* mail -- designate sender */
SmtpPhase
= "server MAIL";
/* check for validity of this command */
/* set sending host to our known value */
sendinghost
= peerhostname
;
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",
if (strcasecmp(sendinghost
, peerhostname
) != 0 &&
(strcasecmp(peerhostname
, "localhost") != 0 ||
strcasecmp(sendinghost
, MyHostName
) != 0))
auth_warning(e
, "Host %s claimed to be %s",
peerhostname
, sendinghost
);
define('r', protocol
, e
);
define('s', sendinghost
, e
);
e
->e_flags
|= EF_LOGSENDER
|EF_CLRQUEUE
;
setproctitle("%s %s: %.80s", e
->e_id
, CurSmtpClient
, 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')
/* check for possible spoofing */
if (RealUid
!= 0 && OpMode
== MD_SMTP
&&
!bitnset(M_LOCALMAILER
, e
->e_from
.q_mailer
->m_flags
) &&
strcmp(e
->e_from
.q_user
, RealUserName
) != 0)
auth_warning(e
, "%s owned process doing -bs",
/* now parse ESMTP arguments */
while (p
!= NULL
&& *p
!= '\0')
/* 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
);
mail_esmtp_args(kp
, vp
, e
);
if (MaxMessageSize
> 0 && e
->e_msgsize
> MaxMessageSize
)
usrerr("552 Message size exceeds fixed maximum message size (%ld)",
if (!enoughspace(e
->e_msgsize
))
message("452 Insufficient disk space; try again later");
message("250 Sender ok");
/* optimize: non-interactive, don't expand aliases */
if (e
->e_sendmode
!= SM_DELIVER
)
e
->e_flags
|= EF_VRFYONLY
;
case CMDRCPT
: /* rcpt -- designate recipient */
usrerr("503 Need MAIL before RCPT");
SmtpPhase
= "server RCPT";
if (setjmp(TopFrame
) > 0)
e
->e_flags
&= ~EF_FATALERRS
;
a
= sendto(p
, 1, (ADDRESS
*) NULL
, 0);
/* no errors during parsing, but might be a duplicate */
if (!bitset(QBADADDR
, a
->q_flags
))
message("250 Recipient ok%s",
bitset(QQUEUEUP
, a
->q_flags
) ?
/* punt -- should keep message in ADDRESS.... */
message("550 Addressee unknown");
case CMDDATA
: /* data -- text of mail */
SmtpPhase
= "server DATA";
message("503 Need MAIL command");
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(InChannel
, TRUE
, doublequeue
, NULL
, e
);
/* make sure we actually do delivery */
e
->e_flags
&= ~EF_CLRQUEUE
;
/* from now on, we have to operate silently */
e
->e_errormode
= EM_MAIL
;
** 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.
e
->e_xfp
= freopen(queuename(e
, 'x'), "w", e
->e_xfp
);
/* make sure it is in the queue */
if (e
->e_sendmode
== SM_QUEUE
)
e
->e_flags
|= EF_KEEPQUEUE
;
/* send to all recipients */
/* issue success message */
message("250 %s Message accepted for delivery", id
);
/* if we just queued, poke it */
if (doublequeue
&& e
->e_sendmode
!= SM_QUEUE
)
(void) dowork(id
, TRUE
, TRUE
, e
);
/* now make it really happen */
if (!Verbose
&& e
->e_sendmode
!= SM_QUEUE
)
/* 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");
/* arrange to ignore any current send list */
e
->e_flags
|= EF_CLRQUEUE
;
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 Cannot VRFY user; try RCPT to attempt delivery (or try finger)");
message("502 Sorry, we do not allow this operation");
syslog(LOG_INFO
, "%s: %s [rejected]",
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", CurSmtpClient
, inp
);
case CMDHELP
: /* help -- give user info */
case CMDNOOP
: /* noop -- do nothing */
case CMDQUIT
: /* quit -- leave mail */
message("221 %s closing connection", MyHostName
);
/* arrange to ignore any current send list */
/* 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 */
case CMDLOGBOGUS
: /* bogus command */
"\"%s\" command from %s (%s)",
c
->cmdname
, peerhostname
,
anynet_ntoa(&RealHostAddr
));
case CMDERROR
: /* unknown command */
if (++badcommands
> MAXBADCOMMANDS
)
message("421 %s Too many bad commands; closing connection",
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 scanning \"%s\"",
while (isascii(*p
) && isspace(*p
))
/* see if the input word matches desired word */
** MAIL_ESMTP_ARGS -- process ESMTP arguments from MAIL line
** kp -- the parameter key.
** vp -- the value of that parameter.
mail_esmtp_args(kp
, vp
, e
)
if (strcasecmp(kp
, "size") == 0)
usrerr("501 SIZE requires a value");
e
->e_msgsize
= strtoul(vp
, (char **) NULL
, 10);
e
->e_msgsize
= strtol(vp
, (char **) NULL
, 10);
else if (strcasecmp(kp
, "body") == 0)
usrerr("501 BODY requires a value");
if (strcasecmp(vp
, "8bitmime") == 0)
else if (strcasecmp(vp
, "7bit") == 0)
usrerr("501 Unknown BODY type %s",
e
->e_bodytype
= newstr(vp
);
else if (strcasecmp(kp
, "envid") == 0)
usrerr("501 ENVID requires a value");
usrerr("501 Syntax error in ENVID parameter value");
usrerr("501 Duplicate ENVID parameter");
else if (strcasecmp(kp
, "ret") == 0)
usrerr("501 RET requires a value");
if (bitset(EF_RET_PARAM
, e
->e_flags
))
usrerr("501 Duplicate RET parameter");
e
->e_flags
|= EF_RET_PARAM
;
if (strcasecmp(vp
, "hdrs") == 0)
e
->e_flags
|= EF_NO_BODY_RETN
;
else if (strcasecmp(vp
, "full") != 0)
usrerr("501 Bad argument \"%s\" to RET", vp
);
usrerr("501 %s parameter unrecognized", kp
);
** RCPT_ESMTP_ARGS -- process ESMTP arguments from RCPT line
** a -- the address corresponding to the To: parameter.
** kp -- the parameter key.
** vp -- the value of that parameter.
rcpt_esmtp_args(a
, kp
, vp
, e
)
if (strcasecmp(kp
, "notify") == 0)
usrerr("501 NOTIFY requires a value");
a
->q_flags
&= ~(QPINGONSUCCESS
|QPINGONFAILURE
|QPINGONDELAY
);
a
->q_flags
|= QHASNOTIFY
;
if (strcasecmp(vp
, "never") == 0)
for (p
= vp
; p
!= NULL
; vp
= p
)
if (strcasecmp(vp
, "success") == 0)
a
->q_flags
|= QPINGONSUCCESS
;
else if (strcasecmp(vp
, "failure") == 0)
a
->q_flags
|= QPINGONFAILURE
;
else if (strcasecmp(vp
, "delay") == 0)
a
->q_flags
|= QPINGONDELAY
;
usrerr("501 Bad argument \"%s\" to NOTIFY",
else if (strcasecmp(kp
, "orcpt") == 0)
usrerr("501 ORCPT requires a value");
usrerr("501 Syntax error in ORCPT parameter value");
usrerr("501 Duplicate ORCPT parameter");
usrerr("501 %s parameter unrecognized", kp
);
** 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
);
syserr("%s: died on signal %d",
/* if we exited on a QUIT command, complete the process */
if (WEXITSTATUS(st
) == EX_QUIT
)
/* open alias database */
** PADDRTREE -- print address tree
** Used by VRFY and EXPD to dump the tree of addresses produced.
** prints the tree in a nice order.
if (!bitset(QDONTSEND
, a
->q_flags
))
if (prev
->q_fullname
!= NULL
)
message("250-", "%s <%s>", prev
->q_fullname
, prev
->q_paddr
);
message("250-", "<%s>", prev
->q_paddr
);
if (prev
->q_fullname
!= NULL
)
message("250", "%s <%s>", prev
->q_fullname
, prev
->q_paddr
);
message("250", "<%s>", prev
->q_paddr
);
message("550", "User unknown");