* Copyright (c) 1983 Eric P. Allman
* Copyright (c) 1988 Regents of the University of California.
* %sccs.include.redist.c%
static char sccsid
[] = "@(#)srvrsmtp.c 6.57 (Berkeley) %G% (with SMTP)";
static char sccsid
[] = "@(#)srvrsmtp.c 6.57 (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 */
/* 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 */
extern ADDRESS
*sendto();
if (fileno(OutChannel
) != fileno(stdout
))
/* arrange for debugging output to go to remote host */
(void) dup2(fileno(OutChannel
), fileno(stdout
));
CurHostName
= RealHostName
;
setproctitle("srvrsmtp %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
= "srvrsmtp cmd read";
setproctitle("srvrsmtp %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
)
if (strcasecmp(p
, RealHostName
) != 0)
auth_warning(e
, "Host %s claimed to be %s",
message("250", "%s Hello %s, pleased to meet you", HostName
, p
);
case CMDMAIL
: /* mail -- designate sender */
/* 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");
"Host %s didn't use HELO protocol",
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)
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 */
/* 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");
/* 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");
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");
/* punt -- should keep message in ADDRESS.... */
message("550 Addressee unknown");
case CMDDATA
: /* data -- text of mail */
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 */
for (a
= e
->e_sendqueue
; a
!= NULL
; a
= a
->q_next
)
if (bitset(QVERIFIED
, a
->q_flags
))
/* collect the text of the message */
collect(TRUE
, a
!= NULL
, 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 && a
== NULL
)
e
->e_errormode
= EM_MAIL
;
e
->e_xfp
= freopen(queuename(e
, 'x'), "w", e
->e_xfp
);
/* send to all recipients */
sendall(e
, Verbose
? SM_DELIVER
: SM_QUEUE
);
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 (a
!= NULL
&& e
->e_sendmode
!= SM_QUEUE
)
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");
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
);
case CMDHELP
: /* help -- give user info */
case CMDNOOP
: /* noop -- do nothing */
case CMDQUIT
: /* quit -- leave mail */
message("221 %s closing connection", MyHostName
);
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");
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("srvrsmtp %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 */
** 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");