1cc5c4241f8aba6c40615fcc633a60b3df8d06e8
SCCSID(@
(#)srvrsmtp.c 3.47 %G% (no SMTP));
SCCSID(@
(#)srvrsmtp.c 3.47 %G%);
** 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 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 CMDDBGQSHOW 10 /* showq -- show send queue (DEBUG) */
# define CMDDBGDEBUG 11 /* debug -- set debug mode */
# define CMDVERB 12 /* verb -- go into verbose mode */
# define CMDDBGKILL 13 /* kill -- kill sendmail */
# define CMDDBGWIZ 14 /* wiz -- become a wizard */
# define CMDONEX 15 /* onex -- sending one transaction only */
# define CMDDBGSHELL 16 /* shell -- give us a shell */
static struct cmd CmdTab
[] =
bool IsWiz
= FALSE
; /* set if we are a wizard */
char *WizWord
= NULL
; /* the wizard word to compare against */
bool InChild
= FALSE
; /* true if running in a subprocess */
bool OneXact
= FALSE
; /* one xaction only this run */
char *RealHostName
= NULL
; /* verified hostname, set in daemon.c */
#define EX_QUIT 22 /* special code for QUIT command */
bool hasmail
; /* mail command received */
int rcps
; /* number of recipients */
extern ADDRESS
*sendto();
if (OutChannel
!= stdout
)
/* arrange for debugging output to go to remote host */
(void) dup(fileno(OutChannel
));
expand("$e", inp
, &inp
[sizeof inp
], CurEnv
);
/* read the input line */
p
= sfgets(inp
, sizeof inp
, InChannel
);
/* end of file, just die */
message("421", "%s Lost input channel", HostName
);
/* 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
++)
while (*++p
!= '\0' && !isspace(*p
))
for (c
= CmdTab
; c
->cmdname
!= NULL
; c
++)
if (sameword(c
->cmdname
, cmd
))
case CMDHELO
: /* hello -- introduce yourself */
if (RealHostName
!= NULL
&& !sameword(p
, RealHostName
))
(void) sprintf(buf
, "%s (%s)", p
, RealHostName
);
define('s', newstr(buf
), CurEnv
);
define('s', newstr(p
), CurEnv
);
message("250", "%s Hello %s, pleased to meet you", HostName
, p
);
case CMDMAIL
: /* mail -- designate sender */
/* force a sending host even if no HELO given */
if (RealHostName
!= NULL
&& macvalue('s', CurEnv
) == NULL
)
define('s', RealHostName
, CurEnv
);
/* 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)
/* child -- go do the processing */
message("250", "Sender ok");
case CMDRCPT
: /* rcpt -- designate recipient */
a
= sendto(p
, 1, (ADDRESS
*) NULL
, 0);
message("250", "%s... Recipient ok", p
);
case CMDDATA
: /* data -- text of mail */
message("503", "Need MAIL command");
message("503", "Need RCPT (recipient)");
/* collect the text of the message */
** 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.
CurEnv
->e_flags
&= ~EF_FATALERRS
;
CurEnv
->e_xfp
= freopen(queuename(CurEnv
, 'x'), "w", CurEnv
->e_xfp
);
/* send to all recipients */
sendall(CurEnv
, SendMode
);
/* issue success if appropriate and reset */
if (Errors
== 0 || HoldErrs
)
CurEnv
->e_flags
&= ~EF_FATALERRS
;
/* if in a child, pop back to our parent */
case CMDRSET
: /* rset -- reset state */
message("250", "Reset state");
case CMDVRFY
: /* vrfy -- verify address */
if (runinchild("SMTP-VRFY") > 0)
case CMDHELP
: /* help -- give user info */
case CMDNOOP
: /* noop -- do nothing */
case CMDQUIT
: /* quit -- leave mail */
message("221", "%s closing connection", HostName
);
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");
case CMDDBGKILL
: /* kill the parent */
if (kill(MotherPid
, SIGTERM
) >= 0)
message("200", "Mother is dead");
message("500", "Can't kill Mom");
case CMDDBGSHELL
: /* give us an interactive shell */
if (fileno(InChannel
) != 0)
(void) dup(fileno(InChannel
));
if (fileno(InChannel
) != fileno(OutChannel
))
(void) fclose(InChannel
);
if (fileno(OutChannel
) != 1)
(void) dup(fileno(OutChannel
));
(void) fclose(OutChannel
);
execl("/bin/csh", "sendmail", 0);
execl("/bin/sh", "sendmail", 0);
case CMDDBGWIZ
: /* become a wizard */
strncpy(seed
, WizWord
, 2);
if (strcmp(WizWord
, crypt(p
, seed
)) != 0)
message("500", "You are no wizard!");
message("200", "Please pass, oh mighty wizard");
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");
** ISWIZ -- tell us if we are a wizard
** If not, print a nasty message.
** TRUE if we are a wizard.
** FALSE if we are not a wizard.
** Prints a 500 exit stat if we are not a wizard.
message("500", "Mere mortals musn't mutter that mantra");
** 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))
** 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");