1999-01-12 12:38:06 +00:00
|
|
|
/*
|
2005-02-14 02:39:14 +00:00
|
|
|
* Copyright (c) 1998-2004 Sendmail, Inc. and its suppliers.
|
2000-08-12 22:19:16 +00:00
|
|
|
* All rights reserved.
|
1995-12-02 17:30:23 +00:00
|
|
|
* Copyright (c) 1990, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
1998-08-03 05:56:20 +00:00
|
|
|
* By using this file, you agree to the terms and conditions set
|
|
|
|
* forth in the LICENSE file which can be found at the top level of
|
|
|
|
* the sendmail distribution.
|
1995-12-02 17:30:23 +00:00
|
|
|
*
|
2002-08-28 18:12:33 +00:00
|
|
|
* $FreeBSD$
|
|
|
|
*
|
1995-12-02 17:30:23 +00:00
|
|
|
*/
|
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
#include <sm/gen.h>
|
|
|
|
|
|
|
|
SM_IDSTR(copyright,
|
2005-02-14 02:39:14 +00:00
|
|
|
"@(#) Copyright (c) 1998-2004 Sendmail, Inc. and its suppliers.\n\
|
2000-08-12 22:19:16 +00:00
|
|
|
All rights reserved.\n\
|
|
|
|
Copyright (c) 1990, 1993, 1994\n\
|
2002-02-17 21:58:34 +00:00
|
|
|
The Regents of the University of California. All rights reserved.\n")
|
|
|
|
|
2008-08-28 04:33:50 +00:00
|
|
|
SM_IDSTR(id, "@(#)$Id: mail.local.c,v 8.256 2008/02/19 07:13:30 gshapiro Exp $")
|
1995-12-02 17:30:23 +00:00
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sm/errstring.h>
|
|
|
|
#include <sm/io.h>
|
|
|
|
#include <sm/limits.h>
|
|
|
|
# include <unistd.h>
|
|
|
|
# ifdef EX_OK
|
|
|
|
# undef EX_OK /* unistd.h may have another use for this */
|
|
|
|
# endif /* EX_OK */
|
2003-09-19 23:14:57 +00:00
|
|
|
# define LOCKFILE_PMODE 0
|
2002-02-17 21:58:34 +00:00
|
|
|
#include <sm/mbdb.h>
|
|
|
|
#include <sm/sysexits.h>
|
2000-08-12 22:19:16 +00:00
|
|
|
|
2004-08-01 01:16:16 +00:00
|
|
|
#ifndef HASHSPOOL
|
|
|
|
# define HASHSPOOL 0
|
|
|
|
#endif /* ! HASHSPOOL */
|
|
|
|
#ifndef HASHSPOOLMD5
|
|
|
|
# define HASHSPOOLMD5 0
|
|
|
|
#endif /* ! HASHSPOOLMD5 */
|
|
|
|
|
1995-12-02 17:30:23 +00:00
|
|
|
/*
|
2000-08-12 22:19:16 +00:00
|
|
|
** This is not intended to work on System V derived systems
|
|
|
|
** such as Solaris or HP-UX, since they use a totally different
|
2002-02-17 21:58:34 +00:00
|
|
|
** approach to mailboxes (essentially, they have a set-group-ID program
|
|
|
|
** rather than set-user-ID, and they rely on the ability to "give away"
|
2000-08-12 22:19:16 +00:00
|
|
|
** files to do their work). IT IS NOT A BUG that this doesn't
|
|
|
|
** work on such architectures.
|
|
|
|
*/
|
1995-12-02 17:30:23 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <stdlib.h>
|
2000-10-01 02:03:50 +00:00
|
|
|
# include <sys/socket.h>
|
|
|
|
# include <sys/file.h>
|
|
|
|
# include <netinet/in.h>
|
|
|
|
# include <arpa/nameser.h>
|
|
|
|
# include <netdb.h>
|
2002-02-17 21:58:34 +00:00
|
|
|
# include <pwd.h>
|
2000-08-12 22:19:16 +00:00
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
#include <sm/string.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <ctype.h>
|
1998-08-03 05:56:20 +00:00
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
#include <sm/conf.h>
|
|
|
|
#include <sendmail/pathnames.h>
|
1996-10-24 04:51:14 +00:00
|
|
|
|
2004-08-01 01:16:16 +00:00
|
|
|
#if HASHSPOOL
|
|
|
|
# define HASH_NONE 0
|
|
|
|
# define HASH_USER 1
|
|
|
|
# if HASHSPOOLMD5
|
|
|
|
# define HASH_MD5 2
|
|
|
|
# include <openssl/md5.h>
|
|
|
|
# endif /* HASHSPOOLMD5 */
|
|
|
|
#endif /* HASHSPOOL */
|
|
|
|
|
2008-08-28 04:33:50 +00:00
|
|
|
#if _FFR_SPOOL_PATH
|
|
|
|
/*
|
|
|
|
** Override path to mail store at run time (using -p).
|
|
|
|
** From: Eugene Grosbein of Svyaz Service JSC
|
|
|
|
** See: http://www.freebsd.org/cgi/query-pr.cgi?pr=bin/114195
|
|
|
|
** NOTE: Update man page before adding this to a release.
|
|
|
|
*/
|
|
|
|
#endif /* _FFR_SPOOL_PATH */
|
|
|
|
|
1998-08-03 05:56:20 +00:00
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
#ifndef LOCKTO_RM
|
|
|
|
# define LOCKTO_RM 300 /* timeout for stale lockfile removal */
|
|
|
|
#endif /* ! LOCKTO_RM */
|
|
|
|
#ifndef LOCKTO_GLOB
|
|
|
|
# define LOCKTO_GLOB 400 /* global timeout for lockfile creation */
|
|
|
|
#endif /* ! LOCKTO_GLOB */
|
|
|
|
|
|
|
|
/* define a realloc() which works for NULL pointers */
|
|
|
|
#define REALLOC(ptr, size) (((ptr) == NULL) ? malloc(size) : realloc(ptr, size))
|
1995-12-02 17:30:23 +00:00
|
|
|
|
1998-08-03 05:56:20 +00:00
|
|
|
/*
|
2002-02-17 21:58:34 +00:00
|
|
|
** If you don't have flock, you could try using lockf instead.
|
2000-08-12 22:19:16 +00:00
|
|
|
*/
|
2001-02-28 00:22:47 +00:00
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
#ifdef LDA_USE_LOCKF
|
|
|
|
# define flock(a, b) lockf(a, b, 0)
|
|
|
|
# ifdef LOCK_EX
|
|
|
|
# undef LOCK_EX
|
|
|
|
# endif /* LOCK_EX */
|
|
|
|
# define LOCK_EX F_LOCK
|
|
|
|
#endif /* LDA_USE_LOCKF */
|
1998-08-03 05:56:20 +00:00
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
#ifndef LOCK_EX
|
|
|
|
# include <sys/file.h>
|
|
|
|
#endif /* ! LOCK_EX */
|
1995-12-02 17:30:23 +00:00
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
/*
|
|
|
|
** If you don't have setreuid, and you have saved uids, and you have
|
|
|
|
** a seteuid() call that doesn't try to emulate using setuid(), then
|
|
|
|
** you can try defining LDA_USE_SETEUID.
|
|
|
|
*/
|
2000-08-12 22:19:16 +00:00
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
#ifdef LDA_USE_SETEUID
|
|
|
|
# define setreuid(r, e) seteuid(e)
|
|
|
|
#endif /* LDA_USE_SETEUID */
|
2000-10-01 02:03:50 +00:00
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
#ifdef LDA_CONTENTLENGTH
|
|
|
|
# define CONTENTLENGTH 1
|
|
|
|
#endif /* LDA_CONTENTLENGTH */
|
2000-08-12 22:19:16 +00:00
|
|
|
|
|
|
|
#ifndef INADDRSZ
|
|
|
|
# define INADDRSZ 4 /* size of an IPv4 address in bytes */
|
|
|
|
#endif /* ! INADDRSZ */
|
1995-12-02 17:30:23 +00:00
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
#ifdef MAILLOCK
|
|
|
|
# include <maillock.h>
|
|
|
|
#endif /* MAILLOCK */
|
|
|
|
|
1999-01-12 12:38:06 +00:00
|
|
|
#ifndef MAILER_DAEMON
|
|
|
|
# define MAILER_DAEMON "MAILER-DAEMON"
|
2000-08-12 22:19:16 +00:00
|
|
|
#endif /* ! MAILER_DAEMON */
|
|
|
|
|
|
|
|
#ifdef CONTENTLENGTH
|
|
|
|
char ContentHdr[40] = "Content-Length: ";
|
|
|
|
off_t HeaderLength;
|
|
|
|
off_t BodyLength;
|
|
|
|
#endif /* CONTENTLENGTH */
|
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
bool EightBitMime = true; /* advertise 8BITMIME in LMTP */
|
2001-02-28 00:22:47 +00:00
|
|
|
char ErrBuf[10240]; /* error buffer */
|
2000-08-12 22:19:16 +00:00
|
|
|
int ExitVal = EX_OK; /* sysexits.h error value. */
|
2002-02-17 21:58:34 +00:00
|
|
|
bool nobiff = false;
|
|
|
|
bool nofsync = false;
|
|
|
|
bool HoldErrs = false; /* Hold errors in ErrBuf */
|
|
|
|
bool LMTPMode = false;
|
|
|
|
bool BounceQuota = false; /* permanent error when over quota */
|
2005-02-14 02:39:14 +00:00
|
|
|
bool CloseMBDB = false;
|
2002-02-17 21:58:34 +00:00
|
|
|
char *HomeMailFile = NULL; /* store mail in homedir */
|
2000-08-12 22:19:16 +00:00
|
|
|
|
2004-08-01 01:16:16 +00:00
|
|
|
#if HASHSPOOL
|
|
|
|
int HashType = HASH_NONE;
|
|
|
|
int HashDepth = 0;
|
|
|
|
bool StripRcptDomain = true;
|
|
|
|
#else /* HASHSPOOL */
|
|
|
|
# define StripRcptDomain true
|
|
|
|
#endif /* HASHSPOOL */
|
|
|
|
char SpoolPath[MAXPATHLEN];
|
|
|
|
|
2005-02-14 02:39:14 +00:00
|
|
|
char *parseaddr __P((char *, bool));
|
|
|
|
char *process_recipient __P((char *));
|
|
|
|
void dolmtp __P((void));
|
2001-02-28 00:22:47 +00:00
|
|
|
void deliver __P((int, char *));
|
2000-08-12 22:19:16 +00:00
|
|
|
int e_to_sys __P((int));
|
|
|
|
void notifybiff __P((char *));
|
2002-02-17 21:58:34 +00:00
|
|
|
int store __P((char *, bool *));
|
2000-08-12 22:19:16 +00:00
|
|
|
void usage __P((void));
|
|
|
|
int lockmbox __P((char *));
|
|
|
|
void unlockmbox __P((void));
|
|
|
|
void mailerr __P((const char *, const char *, ...));
|
2001-02-28 00:22:47 +00:00
|
|
|
void flush_error __P((void));
|
2004-08-01 01:16:16 +00:00
|
|
|
#if HASHSPOOL
|
|
|
|
const char *hashname __P((char *));
|
|
|
|
#endif /* HASHSPOOL */
|
2000-08-12 22:19:16 +00:00
|
|
|
|
1995-12-02 17:30:23 +00:00
|
|
|
|
2007-04-09 01:44:16 +00:00
|
|
|
static void sm_exit __P((int));
|
|
|
|
|
2005-02-14 02:39:14 +00:00
|
|
|
static void
|
|
|
|
sm_exit(status)
|
|
|
|
int status;
|
|
|
|
{
|
|
|
|
if (CloseMBDB)
|
|
|
|
{
|
|
|
|
sm_mbdb_terminate();
|
|
|
|
CloseMBDB = false; /* not really necessary, but ... */
|
|
|
|
}
|
|
|
|
exit(status);
|
|
|
|
}
|
|
|
|
|
1995-12-02 17:30:23 +00:00
|
|
|
int
|
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
|
|
|
{
|
|
|
|
struct passwd *pw;
|
2000-08-12 22:19:16 +00:00
|
|
|
int ch, fd;
|
1995-12-02 17:30:23 +00:00
|
|
|
uid_t uid;
|
|
|
|
char *from;
|
2002-02-17 21:58:34 +00:00
|
|
|
char *mbdbname = "pw";
|
|
|
|
int err;
|
1995-12-02 17:30:23 +00:00
|
|
|
extern char *optarg;
|
|
|
|
extern int optind;
|
2000-08-12 22:19:16 +00:00
|
|
|
|
1995-12-02 17:30:23 +00:00
|
|
|
|
|
|
|
/* make sure we have some open file descriptors */
|
|
|
|
for (fd = 10; fd < 30; fd++)
|
|
|
|
(void) close(fd);
|
|
|
|
|
|
|
|
/* use a reasonable umask */
|
|
|
|
(void) umask(0077);
|
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
# ifdef LOG_MAIL
|
1995-12-02 17:30:23 +00:00
|
|
|
openlog("mail.local", 0, LOG_MAIL);
|
2000-08-12 22:19:16 +00:00
|
|
|
# else /* LOG_MAIL */
|
1995-12-02 17:30:23 +00:00
|
|
|
openlog("mail.local", 0);
|
2000-08-12 22:19:16 +00:00
|
|
|
# endif /* LOG_MAIL */
|
1995-12-02 17:30:23 +00:00
|
|
|
|
|
|
|
from = NULL;
|
2005-02-14 02:39:14 +00:00
|
|
|
|
|
|
|
/* XXX can this be converted to a compile time check? */
|
2004-08-01 01:16:16 +00:00
|
|
|
if (sm_strlcpy(SpoolPath, _PATH_MAILDIR, sizeof(SpoolPath)) >=
|
|
|
|
sizeof(SpoolPath))
|
|
|
|
{
|
|
|
|
mailerr("421", "Configuration error: _PATH_MAILDIR too large");
|
2005-02-14 02:39:14 +00:00
|
|
|
sm_exit(EX_CONFIG);
|
2004-08-01 01:16:16 +00:00
|
|
|
}
|
|
|
|
#if HASHSPOOL
|
|
|
|
while ((ch = getopt(argc, argv, "7BbdD:f:h:r:lH:p:ns")) != -1)
|
|
|
|
#else /* HASHSPOOL */
|
2008-08-28 04:33:50 +00:00
|
|
|
# if _FFR_SPOOL_PATH
|
2008-08-28 06:08:29 +00:00
|
|
|
while ((ch = getopt(argc, argv, "7BbdD:f:h:r:lp:s")) != -1)
|
2008-08-28 04:33:50 +00:00
|
|
|
# else /* _FFR_SPOOL_PATH */
|
2002-02-17 21:58:34 +00:00
|
|
|
while ((ch = getopt(argc, argv, "7BbdD:f:h:r:ls")) != -1)
|
2008-08-28 04:33:50 +00:00
|
|
|
# endif /* _FFR_SPOOL_PATH */
|
2004-08-01 01:16:16 +00:00
|
|
|
#endif /* HASHSPOOL */
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
|
|
|
switch(ch)
|
|
|
|
{
|
|
|
|
case '7': /* Do not advertise 8BITMIME */
|
2002-02-17 21:58:34 +00:00
|
|
|
EightBitMime = false;
|
2000-08-12 22:19:16 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'B':
|
2002-02-18 02:18:42 +00:00
|
|
|
nobiff = true;
|
2000-08-12 22:19:16 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'b': /* bounce mail when over quota. */
|
2002-02-17 21:58:34 +00:00
|
|
|
BounceQuota = true;
|
1998-08-04 13:14:21 +00:00
|
|
|
break;
|
2000-08-12 22:19:16 +00:00
|
|
|
|
|
|
|
case 'd': /* Backward compatible. */
|
1995-12-02 17:30:23 +00:00
|
|
|
break;
|
2000-08-12 22:19:16 +00:00
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
case 'D': /* mailbox database type */
|
|
|
|
mbdbname = optarg;
|
|
|
|
break;
|
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
case 'f':
|
|
|
|
case 'r': /* Backward compatible. */
|
|
|
|
if (from != NULL)
|
|
|
|
{
|
2001-02-28 00:22:47 +00:00
|
|
|
mailerr(NULL, "Multiple -f options");
|
1995-12-02 17:30:23 +00:00
|
|
|
usage();
|
|
|
|
}
|
|
|
|
from = optarg;
|
|
|
|
break;
|
2000-08-12 22:19:16 +00:00
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
case 'h':
|
|
|
|
if (optarg != NULL || *optarg != '\0')
|
|
|
|
HomeMailFile = optarg;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mailerr(NULL, "-h: missing filename");
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
case 'l':
|
2002-02-17 21:58:34 +00:00
|
|
|
LMTPMode = true;
|
1998-08-03 05:56:20 +00:00
|
|
|
break;
|
2000-08-12 22:19:16 +00:00
|
|
|
|
|
|
|
case 's':
|
1998-08-04 13:56:32 +00:00
|
|
|
nofsync++;
|
1998-08-04 13:14:21 +00:00
|
|
|
break;
|
2000-08-12 22:19:16 +00:00
|
|
|
|
2004-08-01 01:16:16 +00:00
|
|
|
#if HASHSPOOL
|
|
|
|
case 'H':
|
|
|
|
if (optarg == NULL || *optarg == '\0')
|
|
|
|
{
|
|
|
|
mailerr(NULL, "-H: missing hashinfo");
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
switch(optarg[0])
|
|
|
|
{
|
|
|
|
case 'u':
|
|
|
|
HashType = HASH_USER;
|
|
|
|
break;
|
|
|
|
|
|
|
|
# if HASHSPOOLMD5
|
|
|
|
case 'm':
|
|
|
|
HashType = HASH_MD5;
|
|
|
|
break;
|
|
|
|
# endif /* HASHSPOOLMD5 */
|
|
|
|
|
|
|
|
default:
|
|
|
|
mailerr(NULL, "-H: unknown hash type");
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
if (optarg[1] == '\0')
|
|
|
|
{
|
|
|
|
mailerr(NULL, "-H: invalid hash depth");
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
HashDepth = atoi(&optarg[1]);
|
|
|
|
if ((HashDepth <= 0) || ((HashDepth * 2) >= MAXPATHLEN))
|
|
|
|
{
|
|
|
|
mailerr(NULL, "-H: invalid hash depth");
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2008-08-28 04:33:50 +00:00
|
|
|
case 'n':
|
|
|
|
StripRcptDomain = false;
|
|
|
|
break;
|
|
|
|
#endif /* HASHSPOOL */
|
|
|
|
|
|
|
|
#if HASHSPOOL || _FFR_SPOOL_PATH
|
2004-08-01 01:16:16 +00:00
|
|
|
case 'p':
|
|
|
|
if (optarg == NULL || *optarg == '\0')
|
|
|
|
{
|
|
|
|
mailerr(NULL, "-p: missing spool path");
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
if (sm_strlcpy(SpoolPath, optarg, sizeof(SpoolPath)) >=
|
|
|
|
sizeof(SpoolPath))
|
|
|
|
{
|
|
|
|
mailerr(NULL, "-p: invalid spool path");
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
break;
|
2008-08-28 04:33:50 +00:00
|
|
|
#endif /* HASHSPOOL || _FFR_SPOOL_PATH */
|
2004-08-01 01:16:16 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
case '?':
|
|
|
|
default:
|
1995-12-02 17:30:23 +00:00
|
|
|
usage();
|
|
|
|
}
|
2000-08-12 22:19:16 +00:00
|
|
|
}
|
1995-12-02 17:30:23 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
/* initialize biff structures */
|
|
|
|
if (!nobiff)
|
|
|
|
notifybiff(NULL);
|
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
err = sm_mbdb_initialize(mbdbname);
|
|
|
|
if (err != EX_OK)
|
|
|
|
{
|
|
|
|
char *errcode = "521";
|
|
|
|
|
|
|
|
if (err == EX_TEMPFAIL)
|
|
|
|
errcode = "421";
|
|
|
|
|
|
|
|
mailerr(errcode, "Can not open mailbox database %s: %s",
|
|
|
|
mbdbname, sm_strexit(err));
|
2005-02-14 02:39:14 +00:00
|
|
|
sm_exit(err);
|
2002-02-17 21:58:34 +00:00
|
|
|
}
|
2005-02-14 02:39:14 +00:00
|
|
|
CloseMBDB = true;
|
2002-02-17 21:58:34 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
if (LMTPMode)
|
2001-02-28 00:22:47 +00:00
|
|
|
{
|
|
|
|
if (argc > 0)
|
|
|
|
{
|
|
|
|
mailerr("421", "Users should not be specified in command line if LMTP required");
|
2005-02-14 02:39:14 +00:00
|
|
|
sm_exit(EX_TEMPFAIL);
|
2001-02-28 00:22:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dolmtp();
|
|
|
|
/* NOTREACHED */
|
2005-02-14 02:39:14 +00:00
|
|
|
sm_exit(EX_OK);
|
2001-02-28 00:22:47 +00:00
|
|
|
}
|
1998-08-03 05:56:20 +00:00
|
|
|
|
2001-02-28 00:22:47 +00:00
|
|
|
/* Non-LMTP from here on out */
|
2000-08-12 22:19:16 +00:00
|
|
|
if (*argv == '\0')
|
1995-12-02 17:30:23 +00:00
|
|
|
usage();
|
|
|
|
|
|
|
|
/*
|
2000-08-12 22:19:16 +00:00
|
|
|
** If from not specified, use the name from getlogin() if the
|
|
|
|
** uid matches, otherwise, use the name from the password file
|
|
|
|
** corresponding to the uid.
|
|
|
|
*/
|
2001-02-28 00:22:47 +00:00
|
|
|
|
1995-12-02 17:30:23 +00:00
|
|
|
uid = getuid();
|
2000-08-12 22:19:16 +00:00
|
|
|
if (from == NULL && ((from = getlogin()) == NULL ||
|
|
|
|
(pw = getpwnam(from)) == NULL ||
|
|
|
|
pw->pw_uid != uid))
|
|
|
|
from = (pw = getpwuid(uid)) != NULL ? pw->pw_name : "???";
|
1995-12-02 17:30:23 +00:00
|
|
|
|
|
|
|
/*
|
2000-08-12 22:19:16 +00:00
|
|
|
** There is no way to distinguish the error status of one delivery
|
|
|
|
** from the rest of the deliveries. So, if we failed hard on one
|
|
|
|
** or more deliveries, but had no failures on any of the others, we
|
|
|
|
** return a hard failure. If we failed temporarily on one or more
|
|
|
|
** deliveries, we return a temporary failure regardless of the other
|
|
|
|
** failures. This results in the delivery being reattempted later
|
|
|
|
** at the expense of repeated failures and multiple deliveries.
|
|
|
|
*/
|
2001-02-28 00:22:47 +00:00
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
HoldErrs = true;
|
|
|
|
fd = store(from, NULL);
|
|
|
|
HoldErrs = false;
|
2001-02-28 00:22:47 +00:00
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
flush_error();
|
2005-02-14 02:39:14 +00:00
|
|
|
sm_exit(ExitVal);
|
2001-02-28 00:22:47 +00:00
|
|
|
}
|
|
|
|
for (; *argv != NULL; ++argv)
|
|
|
|
deliver(fd, *argv);
|
2005-02-14 02:39:14 +00:00
|
|
|
sm_exit(ExitVal);
|
2000-08-12 22:19:16 +00:00
|
|
|
/* NOTREACHED */
|
|
|
|
return ExitVal;
|
1995-12-02 17:30:23 +00:00
|
|
|
}
|
|
|
|
|
1998-08-03 05:56:20 +00:00
|
|
|
char *
|
2000-08-12 22:19:16 +00:00
|
|
|
parseaddr(s, rcpt)
|
1998-08-03 05:56:20 +00:00
|
|
|
char *s;
|
2000-08-12 22:19:16 +00:00
|
|
|
bool rcpt;
|
1998-08-03 05:56:20 +00:00
|
|
|
{
|
|
|
|
char *p;
|
2000-08-12 22:19:16 +00:00
|
|
|
int l;
|
1998-08-03 05:56:20 +00:00
|
|
|
|
|
|
|
if (*s++ != '<')
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
p = s;
|
|
|
|
|
|
|
|
/* at-domain-list */
|
2000-08-12 22:19:16 +00:00
|
|
|
while (*p == '@')
|
|
|
|
{
|
1998-08-03 05:56:20 +00:00
|
|
|
p++;
|
2000-08-12 22:19:16 +00:00
|
|
|
while (*p != ',' && *p != ':' && *p != '\0')
|
1998-08-03 05:56:20 +00:00
|
|
|
p++;
|
2000-08-12 22:19:16 +00:00
|
|
|
if (*p == '\0')
|
1998-08-03 05:56:20 +00:00
|
|
|
return NULL;
|
2000-08-12 22:19:16 +00:00
|
|
|
|
|
|
|
/* Skip over , or : */
|
|
|
|
p++;
|
1998-08-03 05:56:20 +00:00
|
|
|
}
|
|
|
|
|
1999-01-12 12:38:06 +00:00
|
|
|
s = p;
|
|
|
|
|
1998-08-03 05:56:20 +00:00
|
|
|
/* local-part */
|
2000-08-12 22:19:16 +00:00
|
|
|
while (*p != '\0' && *p != '@' && *p != '>')
|
|
|
|
{
|
|
|
|
if (*p == '\\')
|
|
|
|
{
|
|
|
|
if (*++p == '\0')
|
1998-08-03 05:56:20 +00:00
|
|
|
return NULL;
|
2000-08-12 22:19:16 +00:00
|
|
|
}
|
|
|
|
else if (*p == '\"')
|
|
|
|
{
|
1998-08-03 05:56:20 +00:00
|
|
|
p++;
|
2000-08-12 22:19:16 +00:00
|
|
|
while (*p != '\0' && *p != '\"')
|
|
|
|
{
|
|
|
|
if (*p == '\\')
|
|
|
|
{
|
|
|
|
if (*++p == '\0')
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
if (*p == '\0' || *(p + 1) == '\0')
|
|
|
|
return NULL;
|
1998-08-03 05:56:20 +00:00
|
|
|
}
|
2000-08-12 22:19:16 +00:00
|
|
|
/* +detail ? */
|
|
|
|
if (*p == '+' && rcpt)
|
|
|
|
*p = '\0';
|
|
|
|
p++;
|
1998-08-03 05:56:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* @domain */
|
2000-08-12 22:19:16 +00:00
|
|
|
if (*p == '@')
|
|
|
|
{
|
|
|
|
if (rcpt)
|
|
|
|
*p++ = '\0';
|
|
|
|
while (*p != '\0' && *p != '>')
|
1998-08-03 05:56:20 +00:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
if (*p != '>')
|
1998-08-03 05:56:20 +00:00
|
|
|
return NULL;
|
2000-08-12 22:19:16 +00:00
|
|
|
else
|
|
|
|
*p = '\0';
|
|
|
|
p++;
|
|
|
|
|
|
|
|
if (*p != '\0' && *p != ' ')
|
1998-08-03 05:56:20 +00:00
|
|
|
return NULL;
|
2000-08-12 22:19:16 +00:00
|
|
|
|
|
|
|
if (*s == '\0')
|
1999-01-12 12:38:06 +00:00
|
|
|
s = MAILER_DAEMON;
|
1998-08-03 05:56:20 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
l = strlen(s) + 1;
|
2002-02-17 21:58:34 +00:00
|
|
|
if (l < 0)
|
|
|
|
return NULL;
|
2000-08-12 22:19:16 +00:00
|
|
|
p = malloc(l);
|
|
|
|
if (p == NULL)
|
|
|
|
{
|
2001-02-28 00:22:47 +00:00
|
|
|
mailerr("421 4.3.0", "Memory exhausted");
|
2005-02-14 02:39:14 +00:00
|
|
|
sm_exit(EX_TEMPFAIL);
|
1998-08-03 05:56:20 +00:00
|
|
|
}
|
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_strlcpy(p, s, l);
|
1998-08-03 05:56:20 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
process_recipient(addr)
|
|
|
|
char *addr;
|
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
SM_MBDB_T user;
|
|
|
|
|
|
|
|
switch (sm_mbdb_lookup(addr, &user))
|
|
|
|
{
|
|
|
|
case EX_OK:
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
case EX_NOUSER:
|
2001-02-28 00:22:47 +00:00
|
|
|
return "550 5.1.1 User unknown";
|
2002-02-17 21:58:34 +00:00
|
|
|
|
|
|
|
case EX_TEMPFAIL:
|
|
|
|
return "451 4.3.0 User database failure; retry later";
|
|
|
|
|
|
|
|
default:
|
|
|
|
return "550 5.3.0 User database failure";
|
|
|
|
}
|
1998-08-03 05:56:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define RCPT_GROW 30
|
|
|
|
|
|
|
|
void
|
2001-02-28 00:22:47 +00:00
|
|
|
dolmtp()
|
1998-08-03 05:56:20 +00:00
|
|
|
{
|
|
|
|
char *return_path = NULL;
|
|
|
|
char **rcpt_addr = NULL;
|
|
|
|
int rcpt_num = 0;
|
|
|
|
int rcpt_alloc = 0;
|
2002-02-17 21:58:34 +00:00
|
|
|
bool gotlhlo = false;
|
1998-08-03 05:56:20 +00:00
|
|
|
char *err;
|
|
|
|
int msgfd;
|
|
|
|
char *p;
|
|
|
|
int i;
|
2000-08-12 22:19:16 +00:00
|
|
|
char myhostname[1024];
|
|
|
|
char buf[4096];
|
1998-08-03 05:56:20 +00:00
|
|
|
|
2001-02-28 00:22:47 +00:00
|
|
|
memset(myhostname, '\0', sizeof myhostname);
|
2000-08-12 22:19:16 +00:00
|
|
|
(void) gethostname(myhostname, sizeof myhostname - 1);
|
2001-02-28 00:22:47 +00:00
|
|
|
if (myhostname[0] == '\0')
|
2002-02-17 21:58:34 +00:00
|
|
|
sm_strlcpy(myhostname, "localhost", sizeof myhostname);
|
1998-08-03 05:56:20 +00:00
|
|
|
|
|
|
|
printf("220 %s LMTP ready\r\n", myhostname);
|
2000-08-12 22:19:16 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
(void) fflush(stdout);
|
|
|
|
if (fgets(buf, sizeof(buf) - 1, stdin) == NULL)
|
2005-02-14 02:39:14 +00:00
|
|
|
sm_exit(EX_OK);
|
1998-08-03 05:56:20 +00:00
|
|
|
p = buf + strlen(buf) - 1;
|
|
|
|
if (p >= buf && *p == '\n')
|
|
|
|
*p-- = '\0';
|
|
|
|
if (p >= buf && *p == '\r')
|
|
|
|
*p-- = '\0';
|
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
switch (buf[0])
|
|
|
|
{
|
|
|
|
case 'd':
|
|
|
|
case 'D':
|
2002-02-17 21:58:34 +00:00
|
|
|
if (sm_strcasecmp(buf, "data") == 0)
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
bool inbody = false;
|
2001-02-28 00:22:47 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
if (rcpt_num == 0)
|
|
|
|
{
|
2001-02-28 00:22:47 +00:00
|
|
|
mailerr("503 5.5.1", "No recipients");
|
1998-08-03 05:56:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
2002-02-17 21:58:34 +00:00
|
|
|
HoldErrs = true;
|
|
|
|
msgfd = store(return_path, &inbody);
|
|
|
|
HoldErrs = false;
|
2001-02-28 00:22:47 +00:00
|
|
|
if (msgfd < 0 && !inbody)
|
|
|
|
{
|
|
|
|
flush_error();
|
1998-08-03 05:56:20 +00:00
|
|
|
continue;
|
2001-02-28 00:22:47 +00:00
|
|
|
}
|
1998-08-03 05:56:20 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
for (i = 0; i < rcpt_num; i++)
|
|
|
|
{
|
2001-02-28 00:22:47 +00:00
|
|
|
if (msgfd < 0)
|
|
|
|
{
|
|
|
|
/* print error for rcpt */
|
|
|
|
flush_error();
|
|
|
|
continue;
|
|
|
|
}
|
1998-08-03 05:56:20 +00:00
|
|
|
p = strchr(rcpt_addr[i], '+');
|
|
|
|
if (p != NULL)
|
2001-02-28 00:22:47 +00:00
|
|
|
*p = '\0';
|
|
|
|
deliver(msgfd, rcpt_addr[i]);
|
1998-08-03 05:56:20 +00:00
|
|
|
}
|
2001-02-28 00:22:47 +00:00
|
|
|
if (msgfd >= 0)
|
|
|
|
(void) close(msgfd);
|
1998-08-03 05:56:20 +00:00
|
|
|
goto rset;
|
|
|
|
}
|
|
|
|
goto syntaxerr;
|
2000-08-12 22:19:16 +00:00
|
|
|
/* NOTREACHED */
|
|
|
|
break;
|
1998-08-03 05:56:20 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
case 'l':
|
|
|
|
case 'L':
|
2002-02-17 21:58:34 +00:00
|
|
|
if (sm_strncasecmp(buf, "lhlo ", 5) == 0)
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
|
|
|
/* check for duplicate per RFC 1651 4.2 */
|
|
|
|
if (gotlhlo)
|
|
|
|
{
|
2001-02-28 00:22:47 +00:00
|
|
|
mailerr("503", "%s Duplicate LHLO",
|
2000-08-12 22:19:16 +00:00
|
|
|
myhostname);
|
|
|
|
continue;
|
|
|
|
}
|
2002-02-17 21:58:34 +00:00
|
|
|
gotlhlo = true;
|
2000-08-12 22:19:16 +00:00
|
|
|
printf("250-%s\r\n", myhostname);
|
|
|
|
if (EightBitMime)
|
|
|
|
printf("250-8BITMIME\r\n");
|
|
|
|
printf("250-ENHANCEDSTATUSCODES\r\n");
|
|
|
|
printf("250 PIPELINING\r\n");
|
1998-08-03 05:56:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
goto syntaxerr;
|
2000-08-12 22:19:16 +00:00
|
|
|
/* NOTREACHED */
|
|
|
|
break;
|
1998-08-03 05:56:20 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
case 'm':
|
|
|
|
case 'M':
|
2002-02-17 21:58:34 +00:00
|
|
|
if (sm_strncasecmp(buf, "mail ", 5) == 0)
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
|
|
|
if (return_path != NULL)
|
|
|
|
{
|
2001-02-28 00:22:47 +00:00
|
|
|
mailerr("503 5.5.1",
|
|
|
|
"Nested MAIL command");
|
1998-08-03 05:56:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
2002-06-11 21:16:51 +00:00
|
|
|
if (sm_strncasecmp(buf + 5, "from:", 5) != 0 ||
|
2000-08-12 22:19:16 +00:00
|
|
|
((return_path = parseaddr(buf + 10,
|
2002-02-17 21:58:34 +00:00
|
|
|
false)) == NULL))
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
2001-02-28 00:22:47 +00:00
|
|
|
mailerr("501 5.5.4",
|
|
|
|
"Syntax error in parameters");
|
1998-08-03 05:56:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
2001-02-28 00:22:47 +00:00
|
|
|
printf("250 2.5.0 Ok\r\n");
|
1998-08-03 05:56:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
goto syntaxerr;
|
2000-08-12 22:19:16 +00:00
|
|
|
/* NOTREACHED */
|
|
|
|
break;
|
1998-08-03 05:56:20 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
case 'n':
|
|
|
|
case 'N':
|
2002-02-17 21:58:34 +00:00
|
|
|
if (sm_strcasecmp(buf, "noop") == 0)
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
2001-02-28 00:22:47 +00:00
|
|
|
printf("250 2.0.0 Ok\r\n");
|
1998-08-03 05:56:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
goto syntaxerr;
|
2000-08-12 22:19:16 +00:00
|
|
|
/* NOTREACHED */
|
|
|
|
break;
|
1998-08-03 05:56:20 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
case 'q':
|
|
|
|
case 'Q':
|
2002-02-17 21:58:34 +00:00
|
|
|
if (sm_strcasecmp(buf, "quit") == 0)
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
2001-02-28 00:22:47 +00:00
|
|
|
printf("221 2.0.0 Bye\r\n");
|
2005-02-14 02:39:14 +00:00
|
|
|
sm_exit(EX_OK);
|
1998-08-03 05:56:20 +00:00
|
|
|
}
|
|
|
|
goto syntaxerr;
|
2000-08-12 22:19:16 +00:00
|
|
|
/* NOTREACHED */
|
|
|
|
break;
|
1998-08-03 05:56:20 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
case 'r':
|
|
|
|
case 'R':
|
2002-02-17 21:58:34 +00:00
|
|
|
if (sm_strncasecmp(buf, "rcpt ", 5) == 0)
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
|
|
|
if (return_path == NULL)
|
|
|
|
{
|
2001-02-28 00:22:47 +00:00
|
|
|
mailerr("503 5.5.1",
|
|
|
|
"Need MAIL command");
|
1998-08-03 05:56:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
2000-08-12 22:19:16 +00:00
|
|
|
if (rcpt_num >= rcpt_alloc)
|
|
|
|
{
|
1998-08-03 05:56:20 +00:00
|
|
|
rcpt_alloc += RCPT_GROW;
|
|
|
|
rcpt_addr = (char **)
|
2000-10-01 02:03:50 +00:00
|
|
|
REALLOC((char *) rcpt_addr,
|
2000-08-12 22:19:16 +00:00
|
|
|
rcpt_alloc *
|
|
|
|
sizeof(char **));
|
|
|
|
if (rcpt_addr == NULL)
|
|
|
|
{
|
2001-02-28 00:22:47 +00:00
|
|
|
mailerr("421 4.3.0",
|
|
|
|
"Memory exhausted");
|
2005-02-14 02:39:14 +00:00
|
|
|
sm_exit(EX_TEMPFAIL);
|
1998-08-03 05:56:20 +00:00
|
|
|
}
|
|
|
|
}
|
2002-02-17 21:58:34 +00:00
|
|
|
if (sm_strncasecmp(buf + 5, "to:", 3) != 0 ||
|
2000-08-12 22:19:16 +00:00
|
|
|
((rcpt_addr[rcpt_num] = parseaddr(buf + 8,
|
2004-08-01 01:16:16 +00:00
|
|
|
StripRcptDomain)) == NULL))
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
2001-02-28 00:22:47 +00:00
|
|
|
mailerr("501 5.5.4",
|
|
|
|
"Syntax error in parameters");
|
1998-08-03 05:56:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
2001-02-28 00:22:47 +00:00
|
|
|
err = process_recipient(rcpt_addr[rcpt_num]);
|
|
|
|
if (err != NULL)
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
2001-02-28 00:22:47 +00:00
|
|
|
mailerr(NULL, "%s", err);
|
1998-08-03 05:56:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
rcpt_num++;
|
2001-02-28 00:22:47 +00:00
|
|
|
printf("250 2.1.5 Ok\r\n");
|
1998-08-03 05:56:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
2002-02-17 21:58:34 +00:00
|
|
|
else if (sm_strcasecmp(buf, "rset") == 0)
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
2001-02-28 00:22:47 +00:00
|
|
|
printf("250 2.0.0 Ok\r\n");
|
1998-08-03 05:56:20 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
rset:
|
2001-01-21 22:21:43 +00:00
|
|
|
while (rcpt_num > 0)
|
1998-08-03 05:56:20 +00:00
|
|
|
free(rcpt_addr[--rcpt_num]);
|
|
|
|
if (return_path != NULL)
|
|
|
|
free(return_path);
|
|
|
|
return_path = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
goto syntaxerr;
|
2000-08-12 22:19:16 +00:00
|
|
|
/* NOTREACHED */
|
|
|
|
break;
|
1998-08-03 05:56:20 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
case 'v':
|
|
|
|
case 'V':
|
2002-02-17 21:58:34 +00:00
|
|
|
if (sm_strncasecmp(buf, "vrfy ", 5) == 0)
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
2001-02-28 00:22:47 +00:00
|
|
|
printf("252 2.3.3 Try RCPT to attempt delivery\r\n");
|
1998-08-03 05:56:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
goto syntaxerr;
|
2000-08-12 22:19:16 +00:00
|
|
|
/* NOTREACHED */
|
|
|
|
break;
|
1998-08-03 05:56:20 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
default:
|
1998-08-03 05:56:20 +00:00
|
|
|
syntaxerr:
|
2001-02-28 00:22:47 +00:00
|
|
|
mailerr("500 5.5.2", "Syntax error");
|
1998-08-03 05:56:20 +00:00
|
|
|
continue;
|
2000-08-12 22:19:16 +00:00
|
|
|
/* NOTREACHED */
|
|
|
|
break;
|
1998-08-03 05:56:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-12-02 17:30:23 +00:00
|
|
|
int
|
2002-02-17 21:58:34 +00:00
|
|
|
store(from, inbody)
|
1995-12-02 17:30:23 +00:00
|
|
|
char *from;
|
2001-02-28 00:22:47 +00:00
|
|
|
bool *inbody;
|
1995-12-02 17:30:23 +00:00
|
|
|
{
|
1999-01-12 12:38:06 +00:00
|
|
|
FILE *fp = NULL;
|
1995-12-02 17:30:23 +00:00
|
|
|
time_t tval;
|
2002-02-17 21:58:34 +00:00
|
|
|
bool eline; /* previous line was empty */
|
|
|
|
bool fullline = true; /* current line is terminated */
|
2000-08-12 22:19:16 +00:00
|
|
|
bool prevfl; /* previous line was terminated */
|
1995-12-02 17:30:23 +00:00
|
|
|
char line[2048];
|
2000-08-12 22:19:16 +00:00
|
|
|
int fd;
|
1995-12-02 17:30:23 +00:00
|
|
|
char tmpbuf[sizeof _PATH_LOCTMP + 1];
|
|
|
|
|
2001-02-28 00:22:47 +00:00
|
|
|
if (inbody != NULL)
|
2002-02-17 21:58:34 +00:00
|
|
|
*inbody = false;
|
2001-02-28 00:22:47 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
(void) umask(0077);
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_strlcpy(tmpbuf, _PATH_LOCTMP, sizeof tmpbuf);
|
2001-02-28 00:22:47 +00:00
|
|
|
if ((fd = mkstemp(tmpbuf)) < 0 || (fp = fdopen(fd, "w+")) == NULL)
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
2003-02-08 20:35:51 +00:00
|
|
|
if (fd >= 0)
|
|
|
|
(void) close(fd);
|
2001-02-28 00:22:47 +00:00
|
|
|
mailerr("451 4.3.0", "Unable to open temporary file");
|
|
|
|
return -1;
|
1995-12-02 17:30:23 +00:00
|
|
|
}
|
2000-08-12 22:19:16 +00:00
|
|
|
(void) unlink(tmpbuf);
|
1995-12-02 17:30:23 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
if (LMTPMode)
|
|
|
|
{
|
2001-02-28 00:22:47 +00:00
|
|
|
printf("354 Go ahead\r\n");
|
2000-08-12 22:19:16 +00:00
|
|
|
(void) fflush(stdout);
|
1998-08-03 05:56:20 +00:00
|
|
|
}
|
2001-02-28 00:22:47 +00:00
|
|
|
if (inbody != NULL)
|
2002-02-17 21:58:34 +00:00
|
|
|
*inbody = true;
|
1998-08-03 05:56:20 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
(void) time(&tval);
|
|
|
|
(void) fprintf(fp, "From %s %s", from, ctime(&tval));
|
|
|
|
|
|
|
|
#ifdef CONTENTLENGTH
|
|
|
|
HeaderLength = 0;
|
|
|
|
BodyLength = -1;
|
|
|
|
#endif /* CONTENTLENGTH */
|
1995-12-02 17:30:23 +00:00
|
|
|
|
|
|
|
line[0] = '\0';
|
2002-02-17 21:58:34 +00:00
|
|
|
eline = true;
|
2001-02-28 00:22:47 +00:00
|
|
|
while (fgets(line, sizeof(line), stdin) != (char *) NULL)
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
|
|
|
size_t line_len = 0;
|
|
|
|
int peek;
|
1999-01-12 12:38:06 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
prevfl = fullline; /* preserve state of previous line */
|
|
|
|
while (line[line_len] != '\n' && line_len < sizeof(line) - 2)
|
|
|
|
line_len++;
|
|
|
|
line_len++;
|
1999-01-12 12:38:06 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
/* Check for dot-stuffing */
|
2001-02-28 00:22:47 +00:00
|
|
|
if (prevfl && LMTPMode && line[0] == '.')
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
|
|
|
if (line[1] == '\n' ||
|
|
|
|
(line[1] == '\r' && line[2] == '\n'))
|
1998-08-03 05:56:20 +00:00
|
|
|
goto lmtpdot;
|
2000-08-12 22:19:16 +00:00
|
|
|
memcpy(line, line + 1, line_len);
|
|
|
|
line_len--;
|
1998-08-03 05:56:20 +00:00
|
|
|
}
|
2000-08-12 22:19:16 +00:00
|
|
|
|
|
|
|
/* Check to see if we have the full line from fgets() */
|
2002-02-17 21:58:34 +00:00
|
|
|
fullline = false;
|
2000-08-12 22:19:16 +00:00
|
|
|
if (line_len > 0)
|
|
|
|
{
|
|
|
|
if (line[line_len - 1] == '\n')
|
|
|
|
{
|
|
|
|
if (line_len >= 2 &&
|
|
|
|
line[line_len - 2] == '\r')
|
|
|
|
{
|
|
|
|
line[line_len - 2] = '\n';
|
|
|
|
line[line_len - 1] = '\0';
|
|
|
|
line_len--;
|
|
|
|
}
|
2002-02-17 21:58:34 +00:00
|
|
|
fullline = true;
|
2000-08-12 22:19:16 +00:00
|
|
|
}
|
|
|
|
else if (line[line_len - 1] == '\r')
|
|
|
|
{
|
|
|
|
/* Did we just miss the CRLF? */
|
|
|
|
peek = fgetc(stdin);
|
|
|
|
if (peek == '\n')
|
|
|
|
{
|
|
|
|
line[line_len - 1] = '\n';
|
2002-02-17 21:58:34 +00:00
|
|
|
fullline = true;
|
2000-08-12 22:19:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
(void) ungetc(peek, stdin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2002-02-17 21:58:34 +00:00
|
|
|
fullline = true;
|
2000-08-12 22:19:16 +00:00
|
|
|
|
|
|
|
#ifdef CONTENTLENGTH
|
|
|
|
if (prevfl && line[0] == '\n' && HeaderLength == 0)
|
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
eline = false;
|
2001-02-28 00:22:47 +00:00
|
|
|
if (fp != NULL)
|
|
|
|
HeaderLength = ftell(fp);
|
2000-08-12 22:19:16 +00:00
|
|
|
if (HeaderLength <= 0)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
** shouldn't happen, unless ftell() is
|
|
|
|
** badly broken
|
|
|
|
*/
|
|
|
|
|
|
|
|
HeaderLength = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else /* CONTENTLENGTH */
|
|
|
|
if (prevfl && line[0] == '\n')
|
2002-02-17 21:58:34 +00:00
|
|
|
eline = true;
|
2000-08-12 22:19:16 +00:00
|
|
|
#endif /* CONTENTLENGTH */
|
|
|
|
else
|
|
|
|
{
|
1995-12-02 17:30:23 +00:00
|
|
|
if (eline && line[0] == 'F' &&
|
2001-02-28 00:22:47 +00:00
|
|
|
fp != NULL &&
|
1995-12-02 17:30:23 +00:00
|
|
|
!memcmp(line, "From ", 5))
|
2000-10-01 02:03:50 +00:00
|
|
|
(void) putc('>', fp);
|
2002-02-17 21:58:34 +00:00
|
|
|
eline = false;
|
2000-08-12 22:19:16 +00:00
|
|
|
#ifdef CONTENTLENGTH
|
|
|
|
/* discard existing "Content-Length:" headers */
|
|
|
|
if (prevfl && HeaderLength == 0 &&
|
|
|
|
(line[0] == 'C' || line[0] == 'c') &&
|
2002-02-17 21:58:34 +00:00
|
|
|
sm_strncasecmp(line, ContentHdr, 15) == 0)
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
** be paranoid: clear the line
|
|
|
|
** so no "wrong matches" may occur later
|
|
|
|
*/
|
|
|
|
line[0] = '\0';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif /* CONTENTLENGTH */
|
|
|
|
|
1995-12-02 17:30:23 +00:00
|
|
|
}
|
2001-02-28 00:22:47 +00:00
|
|
|
if (fp != NULL)
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
2001-02-28 00:22:47 +00:00
|
|
|
(void) fwrite(line, sizeof(char), line_len, fp);
|
|
|
|
if (ferror(fp))
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
1999-01-12 12:38:06 +00:00
|
|
|
mailerr("451 4.3.0",
|
2001-02-28 00:22:47 +00:00
|
|
|
"Temporary file write error");
|
2000-08-12 22:19:16 +00:00
|
|
|
(void) fclose(fp);
|
2001-02-28 00:22:47 +00:00
|
|
|
fp = NULL;
|
|
|
|
continue;
|
1998-08-03 05:56:20 +00:00
|
|
|
}
|
1995-12-02 17:30:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-28 00:22:47 +00:00
|
|
|
/* check if an error occurred */
|
|
|
|
if (fp == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (LMTPMode)
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
1998-08-03 05:56:20 +00:00
|
|
|
/* Got a premature EOF -- toss message and exit */
|
2005-02-14 02:39:14 +00:00
|
|
|
sm_exit(EX_OK);
|
1998-08-03 05:56:20 +00:00
|
|
|
}
|
|
|
|
|
1995-12-02 17:30:23 +00:00
|
|
|
/* If message not newline terminated, need an extra. */
|
2001-02-28 00:22:47 +00:00
|
|
|
if (fp != NULL && strchr(line, '\n') == NULL)
|
2000-08-12 22:19:16 +00:00
|
|
|
(void) putc('\n', fp);
|
1998-08-03 05:56:20 +00:00
|
|
|
|
|
|
|
lmtpdot:
|
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
#ifdef CONTENTLENGTH
|
2001-02-28 00:22:47 +00:00
|
|
|
if (fp != NULL)
|
|
|
|
BodyLength = ftell(fp);
|
2000-08-12 22:19:16 +00:00
|
|
|
if (HeaderLength == 0 && BodyLength > 0) /* empty body */
|
|
|
|
{
|
|
|
|
HeaderLength = BodyLength;
|
|
|
|
BodyLength = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
BodyLength = BodyLength - HeaderLength - 1 ;
|
|
|
|
|
|
|
|
if (HeaderLength > 0 && BodyLength >= 0)
|
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_snprintf(line, sizeof line, "%lld\n",
|
|
|
|
(LONGLONG_T) BodyLength);
|
|
|
|
(void) sm_strlcpy(&ContentHdr[16], line,
|
|
|
|
sizeof(ContentHdr) - 16);
|
2000-08-12 22:19:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
BodyLength = -1; /* Something is wrong here */
|
|
|
|
#endif /* CONTENTLENGTH */
|
|
|
|
|
1995-12-02 17:30:23 +00:00
|
|
|
/* Output a newline; note, empty messages are allowed. */
|
2001-02-28 00:22:47 +00:00
|
|
|
if (fp != NULL)
|
|
|
|
(void) putc('\n', fp);
|
1995-12-02 17:30:23 +00:00
|
|
|
|
2001-02-28 00:22:47 +00:00
|
|
|
if (fp == NULL || fflush(fp) == EOF || ferror(fp) != 0)
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
2001-02-28 00:22:47 +00:00
|
|
|
mailerr("451 4.3.0", "Temporary file write error");
|
|
|
|
if (fp != NULL)
|
2000-08-12 22:19:16 +00:00
|
|
|
(void) fclose(fp);
|
2001-02-28 00:22:47 +00:00
|
|
|
return -1;
|
1995-12-02 17:30:23 +00:00
|
|
|
}
|
2000-08-12 22:19:16 +00:00
|
|
|
return fd;
|
1995-12-02 17:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-02-28 00:22:47 +00:00
|
|
|
deliver(fd, name)
|
1995-12-02 17:30:23 +00:00
|
|
|
int fd;
|
|
|
|
char *name;
|
|
|
|
{
|
2000-08-12 22:19:16 +00:00
|
|
|
struct stat fsb;
|
|
|
|
struct stat sb;
|
|
|
|
char path[MAXPATHLEN];
|
2001-01-21 22:21:43 +00:00
|
|
|
int mbfd = -1, nr = 0, nw, off;
|
2002-02-17 21:58:34 +00:00
|
|
|
int exitval;
|
1995-12-02 17:30:23 +00:00
|
|
|
char *p;
|
2001-02-28 00:22:47 +00:00
|
|
|
char *errcode;
|
2003-09-19 23:14:57 +00:00
|
|
|
off_t curoff, cursize;
|
2000-08-12 22:19:16 +00:00
|
|
|
#ifdef CONTENTLENGTH
|
|
|
|
off_t headerbytes;
|
|
|
|
int readamount;
|
|
|
|
#endif /* CONTENTLENGTH */
|
2002-06-11 21:16:51 +00:00
|
|
|
char biffmsg[100], buf[8 * 1024];
|
2002-02-17 21:58:34 +00:00
|
|
|
SM_MBDB_T user;
|
2000-08-12 22:19:16 +00:00
|
|
|
|
1995-12-02 17:30:23 +00:00
|
|
|
/*
|
2000-08-12 22:19:16 +00:00
|
|
|
** Disallow delivery to unknown names -- special mailboxes can be
|
|
|
|
** handled in the sendmail aliases file.
|
|
|
|
*/
|
2001-02-28 00:22:47 +00:00
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
exitval = sm_mbdb_lookup(name, &user);
|
|
|
|
switch (exitval)
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
case EX_OK:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EX_NOUSER:
|
|
|
|
exitval = EX_UNAVAILABLE;
|
|
|
|
mailerr("550 5.1.1", "%s: User unknown", name);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EX_TEMPFAIL:
|
|
|
|
mailerr("451 4.3.0", "%s: User database failure; retry later",
|
|
|
|
name);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
exitval = EX_UNAVAILABLE;
|
|
|
|
mailerr("550 5.3.0", "%s: User database failure", name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exitval != EX_OK)
|
|
|
|
{
|
|
|
|
if (ExitVal != EX_TEMPFAIL)
|
|
|
|
ExitVal = exitval;
|
1995-12-02 17:30:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2002-02-17 21:58:34 +00:00
|
|
|
|
1995-12-02 17:30:23 +00:00
|
|
|
endpwent();
|
|
|
|
|
|
|
|
/*
|
2000-08-12 22:19:16 +00:00
|
|
|
** Keep name reasonably short to avoid buffer overruns.
|
|
|
|
** This isn't necessary on BSD because of the proper
|
|
|
|
** definition of snprintf(), but it can cause problems
|
|
|
|
** on other systems.
|
|
|
|
** Also, clear out any bogus characters.
|
|
|
|
*/
|
1995-12-02 17:30:23 +00:00
|
|
|
|
2004-08-01 01:16:16 +00:00
|
|
|
#if !HASHSPOOL
|
1995-12-02 17:30:23 +00:00
|
|
|
if (strlen(name) > 40)
|
|
|
|
name[40] = '\0';
|
|
|
|
for (p = name; *p != '\0'; p++)
|
|
|
|
{
|
|
|
|
if (!isascii(*p))
|
|
|
|
*p &= 0x7f;
|
|
|
|
else if (!isprint(*p))
|
|
|
|
*p = '.';
|
|
|
|
}
|
2004-08-01 01:16:16 +00:00
|
|
|
#endif /* !HASHSPOOL */
|
1995-12-02 17:30:23 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
if (HomeMailFile == NULL)
|
|
|
|
{
|
2004-08-01 01:16:16 +00:00
|
|
|
if (sm_strlcpyn(path, sizeof(path),
|
|
|
|
#if HASHSPOOL
|
|
|
|
4,
|
|
|
|
#else /* HASHSPOOL */
|
|
|
|
3,
|
|
|
|
#endif /* HASHSPOOL */
|
|
|
|
SpoolPath, "/",
|
|
|
|
#if HASHSPOOL
|
|
|
|
hashname(name),
|
|
|
|
#endif /* HASHSPOOL */
|
|
|
|
name) >= sizeof(path))
|
2002-02-17 21:58:34 +00:00
|
|
|
{
|
|
|
|
exitval = EX_UNAVAILABLE;
|
|
|
|
mailerr("550 5.1.1", "%s: Invalid mailbox path", name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (*user.mbdb_homedir == '\0')
|
|
|
|
{
|
|
|
|
exitval = EX_UNAVAILABLE;
|
|
|
|
mailerr("550 5.1.1", "%s: User missing home directory", name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (sm_snprintf(path, sizeof(path), "%s/%s",
|
|
|
|
user.mbdb_homedir, HomeMailFile) >= sizeof(path))
|
|
|
|
{
|
|
|
|
exitval = EX_UNAVAILABLE;
|
|
|
|
mailerr("550 5.1.1", "%s: Invalid mailbox path", name);
|
|
|
|
return;
|
|
|
|
}
|
2000-08-12 22:19:16 +00:00
|
|
|
|
1995-12-02 17:30:23 +00:00
|
|
|
|
|
|
|
/*
|
2000-08-12 22:19:16 +00:00
|
|
|
** If the mailbox is linked or a symlink, fail. There's an obvious
|
|
|
|
** race here, that the file was replaced with a symbolic link after
|
|
|
|
** the lstat returned, but before the open. We attempt to detect
|
|
|
|
** this by comparing the original stat information and information
|
|
|
|
** returned by an fstat of the file descriptor returned by the open.
|
|
|
|
**
|
|
|
|
** NB: this is a symptom of a larger problem, that the mail spooling
|
|
|
|
** directory is writeable by the wrong users. If that directory is
|
|
|
|
** writeable, system security is compromised for other reasons, and
|
|
|
|
** it cannot be fixed here.
|
|
|
|
**
|
|
|
|
** If we created the mailbox, set the owner/group. If that fails,
|
|
|
|
** just return. Another process may have already opened it, so we
|
|
|
|
** can't unlink it. Historically, binmail set the owner/group at
|
|
|
|
** each mail delivery. We no longer do this, assuming that if the
|
|
|
|
** ownership or permissions were changed there was a reason.
|
|
|
|
**
|
|
|
|
** XXX
|
|
|
|
** open(2) should support flock'ing the file.
|
|
|
|
*/
|
|
|
|
|
1995-12-02 17:30:23 +00:00
|
|
|
tryagain:
|
2000-08-12 22:19:16 +00:00
|
|
|
#ifdef MAILLOCK
|
|
|
|
p = name;
|
|
|
|
#else /* MAILLOCK */
|
|
|
|
p = path;
|
|
|
|
#endif /* MAILLOCK */
|
|
|
|
if ((off = lockmbox(p)) != 0)
|
|
|
|
{
|
|
|
|
if (off == EX_TEMPFAIL || e_to_sys(off) == EX_TEMPFAIL)
|
|
|
|
{
|
|
|
|
ExitVal = EX_TEMPFAIL;
|
2001-02-28 00:22:47 +00:00
|
|
|
errcode = "451 4.3.0";
|
2000-08-12 22:19:16 +00:00
|
|
|
}
|
|
|
|
else
|
2001-02-28 00:22:47 +00:00
|
|
|
errcode = "551 5.3.0";
|
|
|
|
|
|
|
|
mailerr(errcode, "lockmailbox %s failed; error code %d %s",
|
2002-02-17 21:58:34 +00:00
|
|
|
p, off, errno > 0 ? sm_errstring(errno) : "");
|
2000-08-12 22:19:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lstat(path, &sb) < 0)
|
|
|
|
{
|
|
|
|
int save_errno;
|
|
|
|
int mode = S_IRUSR|S_IWUSR;
|
2002-02-17 21:58:34 +00:00
|
|
|
gid_t gid = user.mbdb_gid;
|
2000-08-12 22:19:16 +00:00
|
|
|
|
|
|
|
#ifdef MAILGID
|
|
|
|
(void) umask(0007);
|
|
|
|
gid = MAILGID;
|
|
|
|
mode |= S_IRGRP|S_IWGRP;
|
|
|
|
#endif /* MAILGID */
|
|
|
|
|
2003-09-19 23:14:57 +00:00
|
|
|
mbfd = open(path, O_APPEND|O_CREAT|O_EXCL|O_WRONLY,
|
2000-10-01 02:03:50 +00:00
|
|
|
mode);
|
2000-08-12 22:19:16 +00:00
|
|
|
save_errno = errno;
|
|
|
|
|
1997-06-27 14:53:01 +00:00
|
|
|
if (lstat(path, &sb) < 0)
|
1997-08-04 05:00:07 +00:00
|
|
|
{
|
2000-08-12 22:19:16 +00:00
|
|
|
ExitVal = EX_CANTCREAT;
|
1999-01-12 12:38:06 +00:00
|
|
|
mailerr("550 5.2.0",
|
|
|
|
"%s: lstat: file changed after open", path);
|
1997-08-04 05:00:07 +00:00
|
|
|
goto err1;
|
|
|
|
}
|
2001-02-28 00:22:47 +00:00
|
|
|
if (mbfd < 0)
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
|
|
|
if (save_errno == EEXIST)
|
1995-12-02 17:30:23 +00:00
|
|
|
goto tryagain;
|
2000-10-01 02:03:50 +00:00
|
|
|
|
|
|
|
/* open failed, don't try again */
|
|
|
|
mailerr("450 4.2.0", "%s: %s", path,
|
2002-02-17 21:58:34 +00:00
|
|
|
sm_errstring(save_errno));
|
2000-10-01 02:03:50 +00:00
|
|
|
goto err0;
|
2000-08-12 22:19:16 +00:00
|
|
|
}
|
2002-02-17 21:58:34 +00:00
|
|
|
else if (fchown(mbfd, user.mbdb_uid, gid) < 0)
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
1998-08-03 05:56:20 +00:00
|
|
|
mailerr("451 4.3.0", "chown %u.%u: %s",
|
2002-02-17 21:58:34 +00:00
|
|
|
user.mbdb_uid, gid, name);
|
1995-12-02 17:30:23 +00:00
|
|
|
goto err1;
|
|
|
|
}
|
2000-10-01 02:03:50 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
** open() was successful, now close it so can
|
|
|
|
** be opened as the right owner again.
|
|
|
|
** Paranoia: reset mbdf since the file descriptor
|
|
|
|
** is no longer valid; better safe than sorry.
|
|
|
|
*/
|
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
sb.st_uid = user.mbdb_uid;
|
2000-10-01 02:03:50 +00:00
|
|
|
(void) close(mbfd);
|
|
|
|
mbfd = -1;
|
|
|
|
}
|
2000-08-12 22:19:16 +00:00
|
|
|
}
|
2003-03-03 17:18:01 +00:00
|
|
|
else if (sb.st_nlink != 1)
|
|
|
|
{
|
|
|
|
mailerr("550 5.2.0", "%s: too many links", path);
|
|
|
|
goto err0;
|
|
|
|
}
|
|
|
|
else if (!S_ISREG(sb.st_mode))
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
1998-08-03 05:56:20 +00:00
|
|
|
mailerr("550 5.2.0", "%s: irregular file", path);
|
1995-12-02 17:30:23 +00:00
|
|
|
goto err0;
|
2000-08-12 22:19:16 +00:00
|
|
|
}
|
2002-02-17 21:58:34 +00:00
|
|
|
else if (sb.st_uid != user.mbdb_uid)
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
|
|
|
ExitVal = EX_CANTCREAT;
|
1998-08-03 05:56:20 +00:00
|
|
|
mailerr("550 5.2.0", "%s: wrong ownership (%d)",
|
2002-02-17 21:58:34 +00:00
|
|
|
path, (int) sb.st_uid);
|
1996-10-24 04:51:14 +00:00
|
|
|
goto err0;
|
1995-12-02 17:30:23 +00:00
|
|
|
}
|
|
|
|
|
2000-10-01 02:03:50 +00:00
|
|
|
/* change UID for quota checks */
|
2002-02-17 21:58:34 +00:00
|
|
|
if (setreuid(0, user.mbdb_uid) < 0)
|
2000-10-01 02:03:50 +00:00
|
|
|
{
|
|
|
|
mailerr("450 4.2.0", "setreuid(0, %d): %s (r=%d, e=%d)",
|
2002-02-17 21:58:34 +00:00
|
|
|
(int) user.mbdb_uid, sm_errstring(errno),
|
|
|
|
(int) getuid(), (int) geteuid());
|
2000-10-01 02:03:50 +00:00
|
|
|
goto err1;
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
2002-02-17 21:58:34 +00:00
|
|
|
fprintf(stderr, "new euid = %d\n", (int) geteuid());
|
2000-10-01 02:03:50 +00:00
|
|
|
#endif /* DEBUG */
|
2003-09-19 23:14:57 +00:00
|
|
|
mbfd = open(path, O_APPEND|O_WRONLY, 0);
|
2000-10-01 02:03:50 +00:00
|
|
|
if (mbfd < 0)
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
mailerr("450 4.2.0", "%s: %s", path, sm_errstring(errno));
|
1995-12-02 17:30:23 +00:00
|
|
|
goto err0;
|
2000-08-12 22:19:16 +00:00
|
|
|
}
|
|
|
|
else if (fstat(mbfd, &fsb) < 0 ||
|
|
|
|
fsb.st_nlink != 1 ||
|
|
|
|
sb.st_nlink != 1 ||
|
|
|
|
!S_ISREG(fsb.st_mode) ||
|
|
|
|
sb.st_dev != fsb.st_dev ||
|
|
|
|
sb.st_ino != fsb.st_ino ||
|
2000-10-01 02:03:50 +00:00
|
|
|
# if HAS_ST_GEN && 0 /* AFS returns random values for st_gen */
|
2000-08-12 22:19:16 +00:00
|
|
|
sb.st_gen != fsb.st_gen ||
|
2000-10-01 02:03:50 +00:00
|
|
|
# endif /* HAS_ST_GEN && 0 */
|
2000-08-12 22:19:16 +00:00
|
|
|
sb.st_uid != fsb.st_uid)
|
|
|
|
{
|
|
|
|
ExitVal = EX_TEMPFAIL;
|
1999-01-12 12:38:06 +00:00
|
|
|
mailerr("550 5.2.0", "%s: fstat: file changed after open",
|
|
|
|
path);
|
1997-06-27 14:53:01 +00:00
|
|
|
goto err1;
|
1995-12-02 17:30:23 +00:00
|
|
|
}
|
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
#if 0
|
|
|
|
/*
|
|
|
|
** This code could be reused if we decide to add a
|
|
|
|
** per-user quota field to the sm_mbdb interface.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Fail if the user has a quota specified, and delivery of this
|
|
|
|
** message would exceed that quota. We bounce such failures using
|
|
|
|
** EX_UNAVAILABLE, unless there were internal problems, since
|
|
|
|
** storing immense messages for later retries can cause queueing
|
|
|
|
** issues.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ui.quota > 0)
|
|
|
|
{
|
|
|
|
struct stat dsb;
|
|
|
|
|
|
|
|
if (fstat(fd, &dsb) < 0)
|
|
|
|
{
|
|
|
|
ExitVal = EX_TEMPFAIL;
|
|
|
|
mailerr("451 4.3.0",
|
|
|
|
"%s: fstat: can't stat temporary storage: %s",
|
|
|
|
ui.mailspool, sm_errstring(errno));
|
|
|
|
goto err1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dsb.st_size + sb.st_size + 1 > ui.quota)
|
|
|
|
{
|
|
|
|
ExitVal = EX_UNAVAILABLE;
|
|
|
|
mailerr("551 5.2.2",
|
|
|
|
"%s: Mailbox full or quota exceeded",
|
|
|
|
ui.mailspool);
|
|
|
|
goto err1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* 0 */
|
2000-08-12 22:19:16 +00:00
|
|
|
|
1995-12-02 17:30:23 +00:00
|
|
|
/* Wait until we can get a lock on the file. */
|
2000-08-12 22:19:16 +00:00
|
|
|
if (flock(mbfd, LOCK_EX) < 0)
|
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
mailerr("450 4.2.0", "%s: %s", path, sm_errstring(errno));
|
1995-12-02 17:30:23 +00:00
|
|
|
goto err1;
|
|
|
|
}
|
|
|
|
|
2003-03-29 19:18:07 +00:00
|
|
|
/* Get the starting offset of the new message */
|
2003-02-08 19:25:21 +00:00
|
|
|
curoff = lseek(mbfd, (off_t) 0, SEEK_END);
|
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
if (!nobiff)
|
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_snprintf(biffmsg, sizeof(biffmsg), "%s@%lld\n",
|
|
|
|
name, (LONGLONG_T) curoff);
|
1998-08-04 13:14:21 +00:00
|
|
|
}
|
1995-12-02 17:30:23 +00:00
|
|
|
|
|
|
|
/* Copy the message into the file. */
|
2001-02-28 00:22:47 +00:00
|
|
|
if (lseek(fd, (off_t) 0, SEEK_SET) == (off_t) -1)
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
2001-02-28 00:22:47 +00:00
|
|
|
mailerr("450 4.2.0", "Temporary file: %s",
|
2002-02-17 21:58:34 +00:00
|
|
|
sm_errstring(errno));
|
1995-12-02 17:30:23 +00:00
|
|
|
goto err1;
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
2002-02-17 21:58:34 +00:00
|
|
|
fprintf(stderr, "before writing: euid = %d\n", (int) geteuid());
|
2000-08-12 22:19:16 +00:00
|
|
|
#endif /* DEBUG */
|
|
|
|
#ifdef CONTENTLENGTH
|
|
|
|
headerbytes = (BodyLength >= 0) ? HeaderLength : -1 ;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if (headerbytes == 0)
|
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_snprintf(buf, sizeof buf, "%s", ContentHdr);
|
2000-08-12 22:19:16 +00:00
|
|
|
nr = strlen(buf);
|
|
|
|
headerbytes = -1;
|
|
|
|
readamount = 0;
|
|
|
|
}
|
|
|
|
else if (headerbytes > sizeof(buf) || headerbytes < 0)
|
|
|
|
readamount = sizeof(buf);
|
|
|
|
else
|
|
|
|
readamount = headerbytes;
|
|
|
|
if (readamount != 0)
|
|
|
|
nr = read(fd, buf, readamount);
|
|
|
|
if (nr <= 0)
|
|
|
|
break;
|
|
|
|
if (headerbytes > 0)
|
|
|
|
headerbytes -= nr ;
|
|
|
|
|
|
|
|
#else /* CONTENTLENGTH */
|
1995-12-02 17:30:23 +00:00
|
|
|
while ((nr = read(fd, buf, sizeof(buf))) > 0)
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
|
|
|
#endif /* CONTENTLENGTH */
|
1995-12-02 17:30:23 +00:00
|
|
|
for (off = 0; off < nr; off += nw)
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
|
|
|
if ((nw = write(mbfd, buf + off, nr - off)) < 0)
|
|
|
|
{
|
2001-02-28 00:22:47 +00:00
|
|
|
errcode = "450 4.2.0";
|
2000-08-12 22:19:16 +00:00
|
|
|
#ifdef EDQUOT
|
2001-02-28 00:22:47 +00:00
|
|
|
if (errno == EDQUOT && BounceQuota)
|
|
|
|
errcode = "552 5.2.2";
|
2000-08-12 22:19:16 +00:00
|
|
|
#endif /* EDQUOT */
|
2001-02-28 00:22:47 +00:00
|
|
|
mailerr(errcode, "%s: %s",
|
2002-02-17 21:58:34 +00:00
|
|
|
path, sm_errstring(errno));
|
1995-12-02 17:30:23 +00:00
|
|
|
goto err3;
|
|
|
|
}
|
2000-08-12 22:19:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (nr < 0)
|
|
|
|
{
|
2001-02-28 00:22:47 +00:00
|
|
|
mailerr("450 4.2.0", "Temporary file: %s",
|
2002-02-17 21:58:34 +00:00
|
|
|
sm_errstring(errno));
|
1995-12-02 17:30:23 +00:00
|
|
|
goto err3;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Flush to disk, don't wait for update. */
|
2000-08-12 22:19:16 +00:00
|
|
|
if (!nofsync && fsync(mbfd) < 0)
|
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
mailerr("450 4.2.0", "%s: %s", path, sm_errstring(errno));
|
1995-12-02 17:30:23 +00:00
|
|
|
err3:
|
|
|
|
#ifdef DEBUG
|
2002-02-17 21:58:34 +00:00
|
|
|
fprintf(stderr, "reset euid = %d\n", (int) geteuid());
|
2000-08-12 22:19:16 +00:00
|
|
|
#endif /* DEBUG */
|
2003-02-08 20:35:51 +00:00
|
|
|
if (mbfd >= 0)
|
|
|
|
(void) ftruncate(mbfd, curoff);
|
2000-10-01 02:03:50 +00:00
|
|
|
err1: if (mbfd >= 0)
|
|
|
|
(void) close(mbfd);
|
2003-09-19 23:14:57 +00:00
|
|
|
err0: (void) setreuid(0, 0);
|
|
|
|
unlockmbox();
|
1995-12-02 17:30:23 +00:00
|
|
|
return;
|
|
|
|
}
|
1998-08-03 05:56:20 +00:00
|
|
|
|
2003-09-19 23:14:57 +00:00
|
|
|
/*
|
|
|
|
** Save the current size so if the close() fails below
|
|
|
|
** we can make sure no other process has changed the mailbox
|
|
|
|
** between the failed close and the re-open()/re-lock().
|
|
|
|
** If something else has changed the size, we shouldn't
|
|
|
|
** try to truncate it as we may do more harm then good
|
|
|
|
** (e.g., truncate a later message delivery).
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (fstat(mbfd, &sb) < 0)
|
|
|
|
cursize = 0;
|
|
|
|
else
|
|
|
|
cursize = sb.st_size;
|
|
|
|
|
|
|
|
|
1995-12-02 17:30:23 +00:00
|
|
|
/* Close and check -- NFS doesn't write until the close. */
|
2000-08-12 22:19:16 +00:00
|
|
|
if (close(mbfd))
|
|
|
|
{
|
2001-02-28 00:22:47 +00:00
|
|
|
errcode = "450 4.2.0";
|
2000-08-12 22:19:16 +00:00
|
|
|
#ifdef EDQUOT
|
2001-02-28 00:22:47 +00:00
|
|
|
if (errno == EDQUOT && BounceQuota)
|
|
|
|
errcode = "552 5.2.2";
|
2000-08-12 22:19:16 +00:00
|
|
|
#endif /* EDQUOT */
|
2002-02-17 21:58:34 +00:00
|
|
|
mailerr(errcode, "%s: %s", path, sm_errstring(errno));
|
2003-09-19 23:14:57 +00:00
|
|
|
mbfd = open(path, O_WRONLY, 0);
|
|
|
|
if (mbfd < 0 ||
|
|
|
|
cursize == 0
|
|
|
|
|| flock(mbfd, LOCK_EX) < 0 ||
|
|
|
|
fstat(mbfd, &sb) < 0 ||
|
|
|
|
sb.st_size != cursize ||
|
2003-02-08 20:35:51 +00:00
|
|
|
sb.st_nlink != 1 ||
|
|
|
|
!S_ISREG(sb.st_mode) ||
|
|
|
|
sb.st_dev != fsb.st_dev ||
|
|
|
|
sb.st_ino != fsb.st_ino ||
|
|
|
|
# if HAS_ST_GEN && 0 /* AFS returns random values for st_gen */
|
|
|
|
sb.st_gen != fsb.st_gen ||
|
|
|
|
# endif /* HAS_ST_GEN && 0 */
|
|
|
|
sb.st_uid != fsb.st_uid
|
|
|
|
)
|
|
|
|
{
|
|
|
|
/* Don't use a bogus file */
|
|
|
|
if (mbfd >= 0)
|
|
|
|
{
|
|
|
|
(void) close(mbfd);
|
|
|
|
mbfd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Attempt to truncate back to pre-write size */
|
|
|
|
goto err3;
|
2000-08-12 22:19:16 +00:00
|
|
|
}
|
|
|
|
else if (!nobiff)
|
1997-06-27 14:53:01 +00:00
|
|
|
notifybiff(biffmsg);
|
1995-12-02 17:30:23 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
if (setreuid(0, 0) < 0)
|
|
|
|
{
|
1998-08-03 05:56:20 +00:00
|
|
|
mailerr("450 4.2.0", "setreuid(0, 0): %s",
|
2002-02-17 21:58:34 +00:00
|
|
|
sm_errstring(errno));
|
1998-08-03 05:56:20 +00:00
|
|
|
goto err0;
|
1995-12-02 17:30:23 +00:00
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
2002-02-17 21:58:34 +00:00
|
|
|
fprintf(stderr, "reset euid = %d\n", (int) geteuid());
|
2000-08-12 22:19:16 +00:00
|
|
|
#endif /* DEBUG */
|
1995-12-02 17:30:23 +00:00
|
|
|
unlockmbox();
|
2000-08-12 22:19:16 +00:00
|
|
|
if (LMTPMode)
|
2001-02-28 00:22:47 +00:00
|
|
|
printf("250 2.1.5 %s Ok\r\n", name);
|
1995-12-02 17:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-08-12 22:19:16 +00:00
|
|
|
** user.lock files are necessary for compatibility with other
|
|
|
|
** systems, e.g., when the mail spool file is NFS exported.
|
|
|
|
** Alas, mailbox locking is more than just a local matter.
|
|
|
|
** EPA 11/94.
|
|
|
|
*/
|
1995-12-02 17:30:23 +00:00
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
bool Locked = false;
|
2000-08-12 22:19:16 +00:00
|
|
|
|
|
|
|
#ifdef MAILLOCK
|
|
|
|
int
|
|
|
|
lockmbox(name)
|
|
|
|
char *name;
|
|
|
|
{
|
2000-10-01 02:03:50 +00:00
|
|
|
int r = 0;
|
2000-08-12 22:19:16 +00:00
|
|
|
|
|
|
|
if (Locked)
|
|
|
|
return 0;
|
|
|
|
if ((r = maillock(name, 15)) == L_SUCCESS)
|
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
Locked = true;
|
2000-08-12 22:19:16 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
switch (r)
|
|
|
|
{
|
|
|
|
case L_TMPLOCK: /* Can't create tmp file */
|
|
|
|
case L_TMPWRITE: /* Can't write pid into lockfile */
|
|
|
|
case L_MAXTRYS: /* Failed after retrycnt attempts */
|
|
|
|
errno = 0;
|
|
|
|
r = EX_TEMPFAIL;
|
|
|
|
break;
|
|
|
|
case L_ERROR: /* Check errno for reason */
|
|
|
|
r = errno;
|
|
|
|
break;
|
|
|
|
default: /* other permanent errors */
|
|
|
|
errno = 0;
|
|
|
|
r = EX_UNAVAILABLE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
1995-12-02 17:30:23 +00:00
|
|
|
|
1996-10-24 04:51:14 +00:00
|
|
|
void
|
2000-08-12 22:19:16 +00:00
|
|
|
unlockmbox()
|
|
|
|
{
|
|
|
|
if (Locked)
|
|
|
|
mailunlock();
|
2002-02-17 21:58:34 +00:00
|
|
|
Locked = false;
|
2000-08-12 22:19:16 +00:00
|
|
|
}
|
|
|
|
#else /* MAILLOCK */
|
|
|
|
|
|
|
|
char LockName[MAXPATHLEN];
|
|
|
|
|
|
|
|
int
|
1995-12-02 17:30:23 +00:00
|
|
|
lockmbox(path)
|
|
|
|
char *path;
|
|
|
|
{
|
|
|
|
int statfailed = 0;
|
2000-08-12 22:19:16 +00:00
|
|
|
time_t start;
|
|
|
|
|
|
|
|
if (Locked)
|
|
|
|
return 0;
|
|
|
|
if (strlen(path) + 6 > sizeof LockName)
|
|
|
|
return EX_SOFTWARE;
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_snprintf(LockName, sizeof LockName, "%s.lock", path);
|
2000-08-12 22:19:16 +00:00
|
|
|
(void) time(&start);
|
|
|
|
for (; ; sleep(5))
|
|
|
|
{
|
1995-12-02 17:30:23 +00:00
|
|
|
int fd;
|
|
|
|
struct stat st;
|
|
|
|
time_t now;
|
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
/* global timeout */
|
|
|
|
(void) time(&now);
|
|
|
|
if (now > start + LOCKTO_GLOB)
|
|
|
|
{
|
|
|
|
errno = 0;
|
|
|
|
return EX_TEMPFAIL;
|
|
|
|
}
|
2003-09-19 23:14:57 +00:00
|
|
|
fd = open(LockName, O_WRONLY|O_EXCL|O_CREAT, LOCKFILE_PMODE);
|
2000-08-12 22:19:16 +00:00
|
|
|
if (fd >= 0)
|
|
|
|
{
|
1998-08-03 05:56:20 +00:00
|
|
|
/* defeat lock checking programs which test pid */
|
2000-08-12 22:19:16 +00:00
|
|
|
(void) write(fd, "0", 2);
|
2002-02-17 21:58:34 +00:00
|
|
|
Locked = true;
|
2000-08-12 22:19:16 +00:00
|
|
|
(void) close(fd);
|
|
|
|
return 0;
|
1995-12-02 17:30:23 +00:00
|
|
|
}
|
2000-08-12 22:19:16 +00:00
|
|
|
if (stat(LockName, &st) < 0)
|
|
|
|
{
|
1995-12-02 17:30:23 +00:00
|
|
|
if (statfailed++ > 5)
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
|
|
|
errno = 0;
|
|
|
|
return EX_TEMPFAIL;
|
|
|
|
}
|
1995-12-02 17:30:23 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
statfailed = 0;
|
2000-08-12 22:19:16 +00:00
|
|
|
(void) time(&now);
|
|
|
|
if (now < st.st_ctime + LOCKTO_RM)
|
1995-12-02 17:30:23 +00:00
|
|
|
continue;
|
2000-08-12 22:19:16 +00:00
|
|
|
|
|
|
|
/* try to remove stale lockfile */
|
|
|
|
if (unlink(LockName) < 0)
|
|
|
|
return errno;
|
1995-12-02 17:30:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-10-24 04:51:14 +00:00
|
|
|
void
|
1995-12-02 17:30:23 +00:00
|
|
|
unlockmbox()
|
|
|
|
{
|
2000-08-12 22:19:16 +00:00
|
|
|
if (!Locked)
|
1995-12-02 17:30:23 +00:00
|
|
|
return;
|
2000-08-12 22:19:16 +00:00
|
|
|
(void) unlink(LockName);
|
2002-02-17 21:58:34 +00:00
|
|
|
Locked = false;
|
1995-12-02 17:30:23 +00:00
|
|
|
}
|
2000-08-12 22:19:16 +00:00
|
|
|
#endif /* MAILLOCK */
|
1995-12-02 17:30:23 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
notifybiff(msg)
|
|
|
|
char *msg;
|
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
static bool initialized = false;
|
1995-12-02 17:30:23 +00:00
|
|
|
static int f = -1;
|
|
|
|
struct hostent *hp;
|
|
|
|
struct servent *sp;
|
|
|
|
int len;
|
2000-08-12 22:19:16 +00:00
|
|
|
static struct sockaddr_in addr;
|
|
|
|
|
|
|
|
if (!initialized)
|
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
initialized = true;
|
1995-12-02 17:30:23 +00:00
|
|
|
|
|
|
|
/* Be silent if biff service not available. */
|
2000-08-12 22:19:16 +00:00
|
|
|
if ((sp = getservbyname("biff", "udp")) == NULL ||
|
|
|
|
(hp = gethostbyname("localhost")) == NULL ||
|
|
|
|
hp->h_length != INADDRSZ)
|
1995-12-02 17:30:23 +00:00
|
|
|
return;
|
2000-08-12 22:19:16 +00:00
|
|
|
|
1995-12-02 17:30:23 +00:00
|
|
|
addr.sin_family = hp->h_addrtype;
|
2000-08-12 22:19:16 +00:00
|
|
|
memcpy(&addr.sin_addr, hp->h_addr, INADDRSZ);
|
1995-12-02 17:30:23 +00:00
|
|
|
addr.sin_port = sp->s_port;
|
|
|
|
}
|
2000-08-12 22:19:16 +00:00
|
|
|
|
|
|
|
/* No message, just return */
|
|
|
|
if (msg == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Couldn't initialize addr struct */
|
|
|
|
if (addr.sin_family == AF_UNSPEC)
|
|
|
|
return;
|
|
|
|
|
2001-02-28 00:22:47 +00:00
|
|
|
if (f < 0 && (f = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
|
1995-12-02 17:30:23 +00:00
|
|
|
return;
|
|
|
|
len = strlen(msg) + 1;
|
2000-08-12 22:19:16 +00:00
|
|
|
(void) sendto(f, msg, len, 0, (struct sockaddr *) &addr, sizeof(addr));
|
1995-12-02 17:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
usage()
|
|
|
|
{
|
2000-08-12 22:19:16 +00:00
|
|
|
ExitVal = EX_USAGE;
|
2008-08-28 04:33:50 +00:00
|
|
|
# if _FFR_SPOOL_PATH
|
2008-08-28 06:08:29 +00:00
|
|
|
mailerr(NULL, "usage: mail.local [-7] [-B] [-b] [-d] [-l] [-s] [-f from|-r from] [-h filename] [-p path] user ...");
|
2008-08-28 04:33:50 +00:00
|
|
|
# else /* _FFR_SPOOL_PATH */
|
2002-02-17 21:58:34 +00:00
|
|
|
mailerr(NULL, "usage: mail.local [-7] [-B] [-b] [-d] [-l] [-s] [-f from|-r from] [-h filename] user ...");
|
2008-08-28 04:33:50 +00:00
|
|
|
# endif /* _FFR_SPOOL_PATH */
|
2005-02-14 02:39:14 +00:00
|
|
|
sm_exit(ExitVal);
|
1995-12-02 17:30:23 +00:00
|
|
|
}
|
|
|
|
|
1998-08-03 05:56:20 +00:00
|
|
|
void
|
2002-02-17 21:58:34 +00:00
|
|
|
/*VARARGS2*/
|
1998-08-03 05:56:20 +00:00
|
|
|
#ifdef __STDC__
|
|
|
|
mailerr(const char *hdr, const char *fmt, ...)
|
2000-08-12 22:19:16 +00:00
|
|
|
#else /* __STDC__ */
|
1998-08-03 05:56:20 +00:00
|
|
|
mailerr(hdr, fmt, va_alist)
|
|
|
|
const char *hdr;
|
|
|
|
const char *fmt;
|
|
|
|
va_dcl
|
2000-08-12 22:19:16 +00:00
|
|
|
#endif /* __STDC__ */
|
1998-08-03 05:56:20 +00:00
|
|
|
{
|
2001-02-28 00:22:47 +00:00
|
|
|
size_t len = 0;
|
2002-02-17 21:58:34 +00:00
|
|
|
SM_VA_LOCAL_DECL
|
1998-08-03 05:56:20 +00:00
|
|
|
|
2001-02-28 00:22:47 +00:00
|
|
|
(void) e_to_sys(errno);
|
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
SM_VA_START(ap, fmt);
|
2001-02-28 00:22:47 +00:00
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
if (LMTPMode && hdr != NULL)
|
1998-08-03 05:56:20 +00:00
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
sm_snprintf(ErrBuf, sizeof ErrBuf, "%s ", hdr);
|
|
|
|
len = strlen(ErrBuf);
|
1998-08-03 05:56:20 +00:00
|
|
|
}
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_vsnprintf(&ErrBuf[len], sizeof ErrBuf - len, fmt, ap);
|
|
|
|
SM_VA_END(ap);
|
2001-02-28 00:22:47 +00:00
|
|
|
|
|
|
|
if (!HoldErrs)
|
|
|
|
flush_error();
|
|
|
|
|
|
|
|
/* Log the message to syslog. */
|
|
|
|
if (!LMTPMode)
|
|
|
|
syslog(LOG_ERR, "%s", ErrBuf);
|
1998-08-03 05:56:20 +00:00
|
|
|
}
|
|
|
|
|
1995-12-02 17:30:23 +00:00
|
|
|
void
|
2001-02-28 00:22:47 +00:00
|
|
|
flush_error()
|
1995-12-02 17:30:23 +00:00
|
|
|
{
|
2001-02-28 00:22:47 +00:00
|
|
|
if (LMTPMode)
|
|
|
|
printf("%s\r\n", ErrBuf);
|
|
|
|
else
|
1995-12-02 17:30:23 +00:00
|
|
|
{
|
2001-02-28 00:22:47 +00:00
|
|
|
if (ExitVal != EX_USAGE)
|
|
|
|
(void) fprintf(stderr, "mail.local: ");
|
|
|
|
fprintf(stderr, "%s\n", ErrBuf);
|
1995-12-02 17:30:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-01 01:16:16 +00:00
|
|
|
#if HASHSPOOL
|
|
|
|
const char *
|
|
|
|
hashname(name)
|
|
|
|
char *name;
|
|
|
|
{
|
|
|
|
static char p[MAXPATHLEN];
|
|
|
|
int i;
|
|
|
|
int len;
|
|
|
|
char *str;
|
|
|
|
# if HASHSPOOLMD5
|
|
|
|
char Base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+_";
|
|
|
|
MD5_CTX ctx;
|
|
|
|
unsigned char md5[18];
|
|
|
|
# if MAXPATHLEN <= 24
|
|
|
|
ERROR _MAXPATHLEN <= 24
|
|
|
|
# endif /* MAXPATHLEN <= 24 */
|
|
|
|
char b64[24];
|
|
|
|
MD5_LONG bits;
|
|
|
|
int j;
|
|
|
|
# endif /* HASHSPOOLMD5 */
|
|
|
|
|
|
|
|
if (HashType == HASH_NONE || HashDepth * 2 >= MAXPATHLEN)
|
|
|
|
{
|
|
|
|
p[0] = '\0';
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(HashType)
|
|
|
|
{
|
|
|
|
case HASH_USER:
|
|
|
|
str = name;
|
|
|
|
break;
|
|
|
|
|
|
|
|
# if HASHSPOOLMD5
|
|
|
|
case HASH_MD5:
|
|
|
|
MD5_Init(&ctx);
|
|
|
|
MD5_Update(&ctx, name, strlen(name));
|
|
|
|
MD5_Final(md5, &ctx);
|
|
|
|
md5[16] = 0;
|
|
|
|
md5[17] = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < 6; i++)
|
|
|
|
{
|
|
|
|
bits = (unsigned) md5[(3 * i)] << 16;
|
|
|
|
bits |= (unsigned) md5[(3 * i) + 1] << 8;
|
|
|
|
bits |= (unsigned) md5[(3 * i) + 2];
|
|
|
|
|
|
|
|
for (j = 3; j >= 0; j--)
|
|
|
|
{
|
|
|
|
b64[(4 * i) + j] = Base64[(bits & 0x3f)];
|
|
|
|
bits >>= 6;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b64[22] = '\0';
|
|
|
|
str = b64;
|
|
|
|
break;
|
|
|
|
# endif /* HASHSPOOLMD5 */
|
|
|
|
}
|
|
|
|
|
|
|
|
len = strlen(str);
|
|
|
|
for (i = 0; i < HashDepth; i++)
|
|
|
|
{
|
|
|
|
if (i < len)
|
|
|
|
p[i * 2] = str[i];
|
|
|
|
else
|
|
|
|
p[i * 2] = '_';
|
|
|
|
p[(i * 2) + 1] = '/';
|
|
|
|
}
|
|
|
|
p[HashDepth * 2] = '\0';
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
#endif /* HASHSPOOL */
|
|
|
|
|
1995-12-02 17:30:23 +00:00
|
|
|
/*
|
|
|
|
* e_to_sys --
|
|
|
|
* Guess which errno's are temporary. Gag me.
|
|
|
|
*/
|
2001-02-28 00:22:47 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
int
|
1995-12-02 17:30:23 +00:00
|
|
|
e_to_sys(num)
|
|
|
|
int num;
|
|
|
|
{
|
|
|
|
/* Temporary failures override hard errors. */
|
2000-08-12 22:19:16 +00:00
|
|
|
if (ExitVal == EX_TEMPFAIL)
|
|
|
|
return ExitVal;
|
1995-12-02 17:30:23 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
switch (num) /* Hopefully temporary errors. */
|
|
|
|
{
|
1995-12-02 17:30:23 +00:00
|
|
|
#ifdef EDQUOT
|
2000-08-12 22:19:16 +00:00
|
|
|
case EDQUOT: /* Disc quota exceeded */
|
2001-02-28 00:22:47 +00:00
|
|
|
if (BounceQuota)
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
|
|
|
ExitVal = EX_UNAVAILABLE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
#endif /* EDQUOT */
|
|
|
|
#ifdef EAGAIN
|
|
|
|
case EAGAIN: /* Resource temporarily unavailable */
|
|
|
|
#endif /* EAGAIN */
|
1995-12-02 17:30:23 +00:00
|
|
|
#ifdef EBUSY
|
2000-08-12 22:19:16 +00:00
|
|
|
case EBUSY: /* Device busy */
|
|
|
|
#endif /* EBUSY */
|
1995-12-02 17:30:23 +00:00
|
|
|
#ifdef EPROCLIM
|
2000-08-12 22:19:16 +00:00
|
|
|
case EPROCLIM: /* Too many processes */
|
|
|
|
#endif /* EPROCLIM */
|
1995-12-02 17:30:23 +00:00
|
|
|
#ifdef EUSERS
|
2000-08-12 22:19:16 +00:00
|
|
|
case EUSERS: /* Too many users */
|
|
|
|
#endif /* EUSERS */
|
1995-12-02 17:30:23 +00:00
|
|
|
#ifdef ECONNABORTED
|
2000-08-12 22:19:16 +00:00
|
|
|
case ECONNABORTED: /* Software caused connection abort */
|
|
|
|
#endif /* ECONNABORTED */
|
1995-12-02 17:30:23 +00:00
|
|
|
#ifdef ECONNREFUSED
|
2000-08-12 22:19:16 +00:00
|
|
|
case ECONNREFUSED: /* Connection refused */
|
|
|
|
#endif /* ECONNREFUSED */
|
1995-12-02 17:30:23 +00:00
|
|
|
#ifdef ECONNRESET
|
2000-08-12 22:19:16 +00:00
|
|
|
case ECONNRESET: /* Connection reset by peer */
|
|
|
|
#endif /* ECONNRESET */
|
1995-12-02 17:30:23 +00:00
|
|
|
#ifdef EDEADLK
|
2000-08-12 22:19:16 +00:00
|
|
|
case EDEADLK: /* Resource deadlock avoided */
|
|
|
|
#endif /* EDEADLK */
|
1995-12-02 17:30:23 +00:00
|
|
|
#ifdef EFBIG
|
2000-08-12 22:19:16 +00:00
|
|
|
case EFBIG: /* File too large */
|
|
|
|
#endif /* EFBIG */
|
1995-12-02 17:30:23 +00:00
|
|
|
#ifdef EHOSTDOWN
|
2000-08-12 22:19:16 +00:00
|
|
|
case EHOSTDOWN: /* Host is down */
|
|
|
|
#endif /* EHOSTDOWN */
|
1995-12-02 17:30:23 +00:00
|
|
|
#ifdef EHOSTUNREACH
|
2000-08-12 22:19:16 +00:00
|
|
|
case EHOSTUNREACH: /* No route to host */
|
|
|
|
#endif /* EHOSTUNREACH */
|
1995-12-02 17:30:23 +00:00
|
|
|
#ifdef EMFILE
|
2000-08-12 22:19:16 +00:00
|
|
|
case EMFILE: /* Too many open files */
|
|
|
|
#endif /* EMFILE */
|
1995-12-02 17:30:23 +00:00
|
|
|
#ifdef ENETDOWN
|
2000-08-12 22:19:16 +00:00
|
|
|
case ENETDOWN: /* Network is down */
|
|
|
|
#endif /* ENETDOWN */
|
1995-12-02 17:30:23 +00:00
|
|
|
#ifdef ENETRESET
|
2000-08-12 22:19:16 +00:00
|
|
|
case ENETRESET: /* Network dropped connection on reset */
|
|
|
|
#endif /* ENETRESET */
|
1995-12-02 17:30:23 +00:00
|
|
|
#ifdef ENETUNREACH
|
2000-08-12 22:19:16 +00:00
|
|
|
case ENETUNREACH: /* Network is unreachable */
|
|
|
|
#endif /* ENETUNREACH */
|
1995-12-02 17:30:23 +00:00
|
|
|
#ifdef ENFILE
|
2000-08-12 22:19:16 +00:00
|
|
|
case ENFILE: /* Too many open files in system */
|
|
|
|
#endif /* ENFILE */
|
1995-12-02 17:30:23 +00:00
|
|
|
#ifdef ENOBUFS
|
2000-08-12 22:19:16 +00:00
|
|
|
case ENOBUFS: /* No buffer space available */
|
|
|
|
#endif /* ENOBUFS */
|
1995-12-02 17:30:23 +00:00
|
|
|
#ifdef ENOMEM
|
2000-08-12 22:19:16 +00:00
|
|
|
case ENOMEM: /* Cannot allocate memory */
|
|
|
|
#endif /* ENOMEM */
|
1995-12-02 17:30:23 +00:00
|
|
|
#ifdef ENOSPC
|
2000-08-12 22:19:16 +00:00
|
|
|
case ENOSPC: /* No space left on device */
|
|
|
|
#endif /* ENOSPC */
|
1995-12-02 17:30:23 +00:00
|
|
|
#ifdef EROFS
|
2000-08-12 22:19:16 +00:00
|
|
|
case EROFS: /* Read-only file system */
|
|
|
|
#endif /* EROFS */
|
1995-12-02 17:30:23 +00:00
|
|
|
#ifdef ESTALE
|
2000-08-12 22:19:16 +00:00
|
|
|
case ESTALE: /* Stale NFS file handle */
|
|
|
|
#endif /* ESTALE */
|
1995-12-02 17:30:23 +00:00
|
|
|
#ifdef ETIMEDOUT
|
2000-08-12 22:19:16 +00:00
|
|
|
case ETIMEDOUT: /* Connection timed out */
|
|
|
|
#endif /* ETIMEDOUT */
|
1995-12-02 17:30:23 +00:00
|
|
|
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN && EWOULDBLOCK != EDEADLK
|
2000-08-12 22:19:16 +00:00
|
|
|
case EWOULDBLOCK: /* Operation would block. */
|
|
|
|
#endif /* defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN && EWOULDBLOCK != EDEADLK */
|
|
|
|
ExitVal = EX_TEMPFAIL;
|
1995-12-02 17:30:23 +00:00
|
|
|
break;
|
2000-08-12 22:19:16 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
ExitVal = EX_UNAVAILABLE;
|
1995-12-02 17:30:23 +00:00
|
|
|
break;
|
|
|
|
}
|
2000-08-12 22:19:16 +00:00
|
|
|
return ExitVal;
|
1995-12-02 17:30:23 +00:00
|
|
|
}
|
|
|
|
|
1998-08-03 05:56:20 +00:00
|
|
|
#if defined(ultrix) || defined(_CRAY)
|
1995-12-02 17:30:23 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1987, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
# if defined(LIBC_SCCS) && !defined(lint)
|
1995-12-02 17:30:23 +00:00
|
|
|
static char sccsid[] = "@(#)mktemp.c 8.1 (Berkeley) 6/4/93";
|
2000-08-12 22:19:16 +00:00
|
|
|
# endif /* defined(LIBC_SCCS) && !defined(lint) */
|
1995-12-02 17:30:23 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
# include <sys/types.h>
|
|
|
|
# include <sys/stat.h>
|
|
|
|
# include <fcntl.h>
|
|
|
|
# include <errno.h>
|
|
|
|
# include <stdio.h>
|
|
|
|
# include <ctype.h>
|
1995-12-02 17:30:23 +00:00
|
|
|
|
|
|
|
static int _gettemp();
|
|
|
|
|
|
|
|
mkstemp(path)
|
|
|
|
char *path;
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
return (_gettemp(path, &fd) ? fd : -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
_gettemp(path, doopen)
|
|
|
|
char *path;
|
|
|
|
register int *doopen;
|
|
|
|
{
|
|
|
|
extern int errno;
|
|
|
|
register char *start, *trv;
|
|
|
|
struct stat sbuf;
|
2002-02-17 21:58:34 +00:00
|
|
|
unsigned int pid;
|
1995-12-02 17:30:23 +00:00
|
|
|
|
|
|
|
pid = getpid();
|
|
|
|
for (trv = path; *trv; ++trv); /* extra X's get set to 0's */
|
2000-08-12 22:19:16 +00:00
|
|
|
while (*--trv == 'X')
|
|
|
|
{
|
1995-12-02 17:30:23 +00:00
|
|
|
*trv = (pid % 10) + '0';
|
|
|
|
pid /= 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check the target directory; if you have six X's and it
|
|
|
|
* doesn't exist this runs for a *very* long time.
|
|
|
|
*/
|
2000-08-12 22:19:16 +00:00
|
|
|
for (start = trv + 1;; --trv)
|
|
|
|
{
|
1995-12-02 17:30:23 +00:00
|
|
|
if (trv <= path)
|
|
|
|
break;
|
2000-08-12 22:19:16 +00:00
|
|
|
if (*trv == '/')
|
|
|
|
{
|
1995-12-02 17:30:23 +00:00
|
|
|
*trv = '\0';
|
1997-06-27 14:53:01 +00:00
|
|
|
if (stat(path, &sbuf) < 0)
|
1995-12-02 17:30:23 +00:00
|
|
|
return(0);
|
2000-08-12 22:19:16 +00:00
|
|
|
if (!S_ISDIR(sbuf.st_mode))
|
|
|
|
{
|
1995-12-02 17:30:23 +00:00
|
|
|
errno = ENOTDIR;
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
*trv = '/';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if (doopen)
|
|
|
|
{
|
2000-10-01 02:03:50 +00:00
|
|
|
if ((*doopen = open(path, O_CREAT|O_EXCL|O_RDWR,
|
|
|
|
0600)) >= 0)
|
1995-12-02 17:30:23 +00:00
|
|
|
return(1);
|
|
|
|
if (errno != EEXIST)
|
|
|
|
return(0);
|
|
|
|
}
|
1997-06-27 14:53:01 +00:00
|
|
|
else if (stat(path, &sbuf) < 0)
|
1995-12-02 17:30:23 +00:00
|
|
|
return(errno == ENOENT ? 1 : 0);
|
|
|
|
|
|
|
|
/* tricky little algorithm for backward compatibility */
|
2000-08-12 22:19:16 +00:00
|
|
|
for (trv = start;;)
|
|
|
|
{
|
1995-12-02 17:30:23 +00:00
|
|
|
if (!*trv)
|
|
|
|
return(0);
|
|
|
|
if (*trv == 'z')
|
|
|
|
*trv++ = 'a';
|
2000-08-12 22:19:16 +00:00
|
|
|
else
|
|
|
|
{
|
1998-08-03 05:56:20 +00:00
|
|
|
if (isascii(*trv) && isdigit(*trv))
|
1995-12-02 17:30:23 +00:00
|
|
|
*trv = 'a';
|
|
|
|
else
|
|
|
|
++*trv;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-08-12 22:19:16 +00:00
|
|
|
/* NOTREACHED */
|
1995-12-02 17:30:23 +00:00
|
|
|
}
|
2000-08-12 22:19:16 +00:00
|
|
|
#endif /* defined(ultrix) || defined(_CRAY) */
|