SCCSID(@
(#)main.c 3.64 %G%);
** SENDMAIL -- Post mail to a set of destinations.
** This is the basic mail router. All user mail programs should
** call this routine to actually deliver mail. Sendmail in
** turn calls a bunch of mail servers that do the real work of
** Sendmail is driven by tables defined in conf.c. This
** file will be different from system to system, but the rest
** of the code will be the same. This table could be read in,
** but it seemed nicer to have it compiled in, since deliver-
** mail will potentially be exercised a lot.
** /etc/sendmail [-f name] [-a] [-q] [-v] [-n] [-m] addr ...
** Positional Parameters:
** addr -- the address to deliver the mail to. There
** -f name The mail is from "name" -- used for
** the header in local mail, and to
** deliver reports of failures to.
** -r name Same as -f; however, this flag is
** reserved to indicate special processing
** for remote mail delivery as needed
** in the future. So, network servers
** -Ffullname Select what the full-name should be
** -a This mail should be in ARPANET std
** format (obsolete version).
** -am Called from an FTP "MAIL" command.
** -af Called from an FTP "MLFL" command.
** -n Don't do aliasing. This might be used
** when delivering responses, for
** -em Mail back a response if there was an
** error in processing. This should be
** used when the origin of this message
** -ew Write back a response if the user is
** still logged in, otherwise, act like
** -eq Don't print any error message (just
** -ep (default) Print error messages
** -ee Send BerkNet style errors. This
** is equivalent to MailBack except
** that it has gives zero return code
** (unless there were errors during
** returning). This used to be
** "EchoBack", but you know how the old
** -m In group expansion, send to the
** sender also (stands for the Mail metoo
** -i Do not terminate mail on a line
** -s Save UNIX-like "From" lines on the
** -v Give blow-by-blow description of
** everything that happens.
** -t Read "to" addresses from message.
** Looks at To:, Cc:, and Bcc: lines.
** -I Initialize the DBM alias files from
** the text format files.
** -Cfilename Use alternate configuration file.
** -Afilename Use alternate alias file.
** -DXvalue Define macro X to have value.
** As defined in <sysexits.h>.
** These codes are actually returned from the auxiliary
** mailers; it is their responsibility to make them
** LOG -- if set, everything is logged.
** Eric Allman, UCB/INGRES
int NextMailer
= 0; /* "free" index into Mailer struct */
static char *FullName
; /* sender's full name */
bool verifyonly
= FALSE
; /* only verify names */
bool safecf
= TRUE
; /* this conf file is sys default */
char ibuf
[30]; /* holds HostName */
bool queuemode
= FALSE
; /* process queue requests */
extern time_t convtime();
extern ADDRESS
*recipient();
if (signal(SIGINT
, SIG_IGN
) != SIG_IGN
)
(void) signal(SIGINT
, finis
);
if (signal(SIGHUP
, SIG_IGN
) != SIG_IGN
)
(void) signal(SIGHUP
, finis
);
(void) signal(SIGTERM
, finis
);
if ((i
= open(DEBUGFILE
, 1)) > 0)
while (--argc
> 0 && (p
= *++argv
)[0] == '-')
case 'r': /* obsolete -f flag */
case 'f': /* from address */
if (--argc
<= 0 || *p
== '-')
syserr("No \"from\" person");
syserr("More than one \"from\" person");
case 'F': /* set full name */
if (--argc
<= 0 || *p
== '-')
case 'h': /* hop count */
if (--argc
<= 0 || *p
< '0' || *p
> '9')
syserr("Bad hop count (%s)", p
);
case 'e': /* error message disposition */
case 'p': /* print errors normally */
case 'q': /* be silent about it */
(void) freopen("/dev/null", "w", stdout
);
case 'm': /* mail back */
case 'e': /* do berknet error processing */
case 'w': /* write back (or mail) */
setbuf(stdout
, (char *) NULL
);
printf("Version %s Debug %d\n", Version
, Debug
);
case 'M': /* redefine internal macro */
case 'C': /* select configuration file */
ConfFile
= "sendmail.cf";
case 'A': /* select alias file */
case 'Q': /* select queue dir */
case 'T': /* set timeout interval */
TimeOut
= convtime(&p
[2]);
case 'n': /* don't alias */
case 'I': /* initialize alias DBM file */
case 'm': /* send to me too */
case 'i': /* don't let dot stop me */
case 'V': /* verify only */
case 'a': /* arpanet format */
syserr("I don't speak SMTP");
case 'c': /* don't connect to non-local mailers */
case 's': /* save From lines in headers */
case 'v': /* give blow-by-blow description */
case 't': /* read recipients from message */
case 'D': /* run as a daemon */
/* explicit fall-through */
case 'q': /* run queue files at intervals */
QueueIntvl
= atoi(&p
[1]);
syserr("I don't know about queues");
case 'p': /* fork politely after initial verification */
case 'o': /* take old-style headers (no commas) */
/* at Eric Schmidt's suggestion, this will not be an error....
syserr("Unknown flag %s", p);
... seems that upward compatibility will be easier. */
** Read system control file.
** Extract special fields for local use.
readcf(ConfFile
, safecf
);
/* our name for SMTP codes */
(void) expand("$i", ibuf
, &ibuf
[sizeof ibuf
- 1]);
/* the indices of local and program mailers */
st
= stab("local", ST_MAILER
, ST_FIND
);
syserr("No local mailer defined");
LocalMailer
= st
->s_mailer
;
st
= stab("prog", ST_MAILER
, ST_FIND
);
syserr("No prog mailer defined");
ProgMailer
= st
->s_mailer
;
initaliases(AliasFile
, aliasinit
);
/* print configuration table (or at least part of it) */
for (i
= 0; i
< MAXMAILERS
; i
++)
register struct mailer
*m
= Mailer
[i
];
printf("mailer %d: %s %s %o %s\n", i
, m
->m_name
,
m
->m_mailer
, m
->m_flags
, m
->m_from
);
** If a daemon, wait for a request.
** getrequests will always return in a child.
** If collecting stuff from the queue, go start doing that.
if (!Daemon
&& argc
<= 0 && !GrabTo
)
usrerr("Usage: /etc/sendmail [flags] addr...");
** The Hop count tells us how many times this message has
** been processed by sendmail. If it exceeds some
** fairly large threshold, then we assume that we have
** an infinite forwarding loop and die.
syserr("Infinite forwarding loop (%s->%s)", From
.q_paddr
, *argv
);
** Scan argv and deliver the message to everyone.
** Actually, suppress delivery if we are taking To:
** lines from the message.
/* if we have had errors sofar, drop out now */
if (Errors
> 0 && ExitStat
== EX_OK
)
** If the sender is local, we will default to old style
** headers. Ugh ugh ugh. But oh-so-necessary under the
if (bitset(M_LOCAL
, From
.q_mailer
->m_flags
))
if (!verifyonly
|| GrabTo
)
** If we don't want to wait around for actual delivery, this
** is a good time to fork off.
** We have examined what we can without doing actual
** delivery, so we will inform our caller of
** Since the parent process will go away immediately,
** the child will be caught by init.
** If the fork fails, we will just continue in the
** parent; this is perfectly safe, albeit
** slower than it must be.
Stat
.stat_nf
[From
.q_mailer
->m_mno
]++;
Stat
.stat_bf
[From
.q_mailer
->m_mno
] += kbytes(MsgSize
);
** Arrange that the person who is sending the mail
** will not be expanded (unless explicitly requested).
printf("From person = \"%s\"\n", From
.q_paddr
);
From
.q_flags
|= QDONTSEND
;
** Actually send everything.
** If verifying, just ack.
** SETFROM -- set the person who this message is from
** Under certain circumstances allow the user to say who
** s/he is (using -f or -r). These are:
** 1. The user's uid is zero (root).
** 2. The user's login name is "network" (mail from
** 3. The user's login name is "uucp" (mail from the
** 4. The address the user is trying to claim has a
** "!" character in it (since #3 doesn't do it for
** us if we are dialing out).
** A better check to replace #3 & #4 would be if the
** effective uid is "UUCP" -- this would require me
** to rewrite getpwent to "grab" uucp as it went by,
** make getname more nasty, do another passwd file
** scan, or compile the UID of "UUCP" into the code,
** all of which are reprehensible.
** Assuming all of these fail, we figure out something
** from -- the person it is from.
** realname -- the actual person executing sendmail.
** If NULL, then take whoever we previously
** thought was the from person.
** sets sendmail's notion of who the from person is.
printf("setfrom(%s, %s)\n", from
, realname
);
if (strcmp(realname
, "network") != 0 && strcmp(realname
, "uucp") != 0 &&
(Debug
== 0 || getuid() != geteuid()) &&
index(from
, '!') == NULL
&& getuid() != 0)
/* network sends -r regardless (why why why?) */
/* syserr("%s, you cannot use the -f flag", realname); */
if (from
== NULL
|| parse(from
, &From
, 1) == NULL
)
(void) parse(from
, &From
, 1);
From
.q_home
= getenv("HOME");
** Set up the $r and $s macros to show who it came from.
if (From
.q_host
!= NULL
&& From
.q_host
[0] != '\0')
define('s', From
.q_host
);
/* should determine network type here */
** Rewrite the from person to dispose of possible implicit
pvp
= prescan(from
, '\0');
syserr("cannot prescan from (%s)", from
);
cataddr(pvp
, frombuf
, sizeof frombuf
);
define('f', newstr(frombuf
));
** FINIS -- Clean up and exit.
printf("\n====finis: stat %d\n", ExitStat
);
/* mail back the transcript on errors */
(void) unlink(Transcript
);
syserr("finis: trying to queue %s", InFileName
);
(void) unlink(InFileName
);
** OPENXSCRPT -- Open transcript file
** Creates a transcript file for possible eventual mailing or
** Turns the standard output into a special file
syserr("Can't create %s", p
);
** SETSENDER -- set sendmail's idea of the sender.
** from -- the person we would like to believe this
** Sets the idea of the sender.
register struct passwd
*pw
;
** Figure out the real user executing us.
** Getlogin can return errno != 0 on non-errors.
nofullname
= (from
!= NULL
);
extern struct passwd
*getpwnam();
syserr("Who are you? (name=%s)", p
);
extern struct passwd
*getpwuid();
syserr("Who are you? (uid=%d)", uid
);
if (p
== NULL
|| p
[0] == '\0' || pw
== NULL
)
** Process passwd file entry.
/* run user's .mailcf file */
(void) expand("$z/.mailcf", cfbuf
, &cfbuf
[sizeof cfbuf
- 1]);
if (!nofullname
&& safefile(cfbuf
, getruid(), S_IREAD
))
/* if the user has given fullname already, don't redefine */
FullName
= macvalue('x');
/* extract full name from passwd file */
if (!nofullname
&& (FullName
== NULL
|| FullName
[0] == '\0') &&
pw
!= NULL
&& pw
->pw_gecos
!= NULL
)
if (FullName
!= NULL
&& FullName
[0] != '\0')
** INITSYS -- initialize instantiation of system
** In Daemon mode, this is done in the child.
** Initializes the system macros, some global variables,
** etc. In particular, the current time in various
static char cbuf
[5]; /* holds hop count */
static char dbuf
[30]; /* holds ctime(tbuf) */
static char pbuf
[10]; /* holds pid */
static char tbuf
[10]; /* holds "current" time */
static char ybuf
[10]; /* holds tty id */
/* convert timeout interval to absolute time */
(void) sprintf(pbuf
, "%d", getpid());
(void) sprintf(cbuf
, "%d", HopCount
);
/* time as integer, unix time, arpa time */
(void) sprintf(tbuf
, "%ld", &CurTime
);
(void) strcpy(dbuf
, ctime(&CurTime
));
*index(dbuf
, '\n') = '\0';
if (macvalue('d') == NULL
)
p
= newstr(arpadate(dbuf
));
if (macvalue('a') == NULL
)
if (macvalue('y') == NULL
)
if (rindex(p
, '/') != NULL
)
** INITMACROS -- initialize the macro system
** This just involves defining some macros that are actually
** used internally as metasymbols to be themselves.
** initializes several macros to be themselves.
/* $0 .... are rewrite replacement */
"$$0", "$$1", "$$2", "$$3", "$$4",
"$$5", "$$6", "$$7", "$$8", "$$9",
/* these are important on the LHS */
/* these are RHS metasymbols */
for (pp
= MetaMacros
; *pp
!= NULL
; pp
++)