* Copyright (c) 1983 Eric P. Allman
* Copyright (c) 1988 Regents of the University of California.
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of California, Berkeley. The name of the
* University may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
static char sccsid
[] = "@(#)srvrsmtp.c 5.27 (Berkeley) 1/18/89 (with SMTP)";
static char sccsid
[] = "@(#)srvrsmtp.c 5.27 (Berkeley) 1/18/89 (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 CMDHELP 6 /* help -- give usage info */
# define CMDNOOP 7 /* noop -- do nothing */
# define CMDQUIT 8 /* quit -- close connection and die */
# define CMDHELO 9 /* helo -- be polite */
# define CMDONEX 10 /* onex -- sending one transaction only */
# define CMDVERB 11 /* verb -- go into verbose mode */
/* debugging-only commands, only enabled if SMTPDEBUG is defined */
# define CMDDBGQSHOW 12 /* showq -- show send queue */
# define CMDDBGDEBUG 13 /* 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 hasmail
; /* mail command received */
extern ADDRESS
*recipient();
extern ENVELOPE BlankEnvelope
;
extern ENVELOPE
*newenvelope();
if (OutChannel
!= stdout
)
/* arrange for debugging output to go to remote host */
(void) dup(fileno(OutChannel
));
if (RealHostName
!= NULL
)
CurHostName
= RealHostName
;
setproctitle("srvrsmtp %s", CurHostName
);
CurHostName
= MyHostName
;
expand("\001e", inp
, &inp
[sizeof inp
], CurEnv
);
/* arrange for backout */
if (setjmp(TopFrame
) > 0 && InChild
)
/* read the input line */
p
= sfgets(inp
, sizeof inp
, InChannel
);
/* end of file, just die */
message("421", "%s Lost input channel from %s",
MyHostName
, CurHostName
);
/* clean up end of line */
/* echo command to transcript */
if (CurEnv
->e_xfp
!= NULL
)
fprintf(CurEnv
->e_xfp
, "<<< %s\n", inp
);
for (p
= inp
; isspace(*p
); p
++)
for (cmd
= cmdbuf
; *p
!= '\0' && !isspace(*p
); )
/* throw away leading whitespace */
for (c
= CmdTab
; c
->cmdname
!= NULL
; c
++)
if (!strcasecmp(c
->cmdname
, cmdbuf
))
case CMDHELO
: /* hello -- introduce yourself */
setproctitle("%s: %s", CurHostName
, inp
);
if (!strcasecmp(p
, MyHostName
))
* didn't know about alias,
* or connected to an echo server
message("553", "Local configuration error, hostname not recognized as local");
if (RealHostName
!= NULL
&& strcasecmp(p
, RealHostName
))
(void) sprintf(hostbuf
, "%s (%s)", p
, RealHostName
);
sendinghost
= newstr(hostbuf
);
message("250", "%s Hello %s, pleased to meet you",
MyHostName
, sendinghost
);
case CMDMAIL
: /* mail -- designate sender */
/* force a sending host even if no HELO given */
if (RealHostName
!= NULL
&& macvalue('s', CurEnv
) == NULL
)
sendinghost
= RealHostName
;
/* check for validity of this command */
message("503", "Sender already specified");
syserr("Nested MAIL command");
/* fork a subprocess to process this command */
if (runinchild("SMTP-MAIL") > 0)
define('s', sendinghost
, CurEnv
);
define('r', "SMTP", CurEnv
);
setproctitle("%s %s: %s", CurEnv
->e_id
,
/* child -- go do the processing */
message("250", "Sender ok");
case CMDRCPT
: /* rcpt -- designate recipient */
setproctitle("%s %s: %s", CurEnv
->e_id
,
if (setjmp(TopFrame
) > 0)
CurEnv
->e_flags
&= ~EF_FATALERRS
;
a
= parseaddr(p
, (ADDRESS
*) NULL
, 1, '\0');
a
= recipient(a
, &CurEnv
->e_sendqueue
);
/* 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 (CurEnv
->e_nrcpts
<= 0)
message("503", "Need RCPT (recipient)");
/* collect the text of the message */
setproctitle("%s %s: %s", CurEnv
->e_id
,
** 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 (CurEnv
->e_nrcpts
!= 1)
CurEnv
->e_flags
&= ~EF_FATALERRS
;
CurEnv
->e_xfp
= freopen(queuename(CurEnv
, 'x'), "w", CurEnv
->e_xfp
);
/* send to all recipients */
sendall(CurEnv
, SM_DEFAULT
);
markstats(CurEnv
, (ADDRESS
*) NULL
);
/* issue success if appropriate and reset */
if (Errors
== 0 || HoldErrs
)
CurEnv
->e_flags
&= ~EF_FATALERRS
;
/* if in a child, pop back to our parent */
CurEnv
= newenvelope(CurEnv
);
CurEnv
->e_flags
= BlankEnvelope
.e_flags
;
case CMDRSET
: /* rset -- reset state */
message("250", "Reset state");
case CMDVRFY
: /* vrfy -- verify address */
if (runinchild("SMTP-VRFY") > 0)
setproctitle("%s: %s", CurHostName
, inp
);
sendtolist(p
, (ADDRESS
*) NULL
, &vrfyqueue
);
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
))
if (vrfyqueue
->q_fullname
== NULL
)
message(code
, "<%s>", vrfyqueue
->q_paddr
);
vrfyqueue
->q_fullname
, vrfyqueue
->q_paddr
);
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
);
case CMDVERB
: /* set verbose mode */
message("200", "Verbose mode");
case CMDONEX
: /* doing one transaction only */
message("200", "Only one transaction");
case CMDDBGQSHOW
: /* show queues */
printaddr(CurEnv
->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 */
if (RealHostName
!= NULL
&& LogLevel
> 0)
"\"%s\" command from %s (%s)\n",
c
->cmdname
, RealHostName
,
inet_ntoa(RealHostAddr
.sin_addr
));
case CMDERROR
: /* unknown command */
message("500", "Command unrecognized");
syserr("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 (*p
!= '\0' && *p
!= ':' && !isspace(*p
))
message("501", "Syntax error");
/* see if the input word matches desired word */
** 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");
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 */
syserr("%s: lost child", label
);
/* if we exited on a QUIT command, complete the process */
if (st
== (EX_QUIT
<< 8))
clearenvelope(CurEnv
, FALSE
);
/* open alias database */
initaliases(AliasFile
, FALSE
);