alias.c revision 058561cbaa119a6f2659bc27ef343e1b47266bb2
/*
* Copyright (c) 1998-2003 Sendmail, Inc. and its suppliers.
* All rights reserved.
* Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved.
* Copyright (c) 1988, 1993
* The Regents of the University of California. All rights reserved.
*
* 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.
*
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sendmail.h>
#define SEPARATOR ':'
# define ALIAS_SPEC_SEPARATORS " ,/:"
static int NAliasFileMaps; /* the number of entries in AliasFileMap */
static char *aliaslookup __P((char *, int *, char *));
/*
** ALIAS -- Compute aliases.
**
** Scans the alias file for an alias for the given address.
** If found, it arranges to deliver to the alias list instead.
** Uses libdbm database if -DDBM.
**
** Parameters:
** a -- address to alias.
** sendq -- a pointer to the head of the send queue
** to put the aliases in.
** aliaslevel -- the current alias nesting depth.
** e -- the current envelope.
**
** Returns:
** none
**
** Side Effects:
** Aliases found are expanded.
**
** Deficiencies:
** It should complain about names that are aliased to
** nothing.
*/
void
register ADDRESS *a;
int aliaslevel;
register ENVELOPE *e;
{
register char *p;
char *owner;
/* don't realias already aliased names */
return;
if (NoAlias)
return;
/*
** Look up this name.
**
** If the map was unavailable, we will queue this message
** until the map becomes available; otherwise, we could
** bounce messages inappropriately.
*/
/*
** envelope <> can't be sent to mailing lists, only owner-
** send spam of this type to owner- of the list
** ---- to stop spam from going to mailing lists!
*/
{
/* Look for owner of alias */
{
if (LogLevel > 8)
"possible spam from <> to list: %s, redirected to %s\n",
}
}
#endif /* _FFR_REDIRECTEMPTY */
{
a->q_state = QS_QUEUEUP;
"alias database unavailable");
/* XXX msg only per recipient? */
a->q_message = "alias database unavailable";
return;
}
if (p == NULL)
return;
/*
** Match on Alias.
** Deliver to the target list.
*/
sm_dprintf("%s (%s, %s) aliased to %s\n",
{
a->q_state = QS_VERIFIED;
return;
}
if (LogLevel > 10)
"alias %.100s => %s",
{
sm_dprintf("alias: QS_EXPANDED ");
printaddr(sm_debug_file(), a, false);
}
a->q_state = QS_EXPANDED;
/*
** Always deliver aliased items as the default user.
** Setting q_gid to 0 forces deliver() to use DefUser
** instead of the alias name for the call to initgroups().
*/
a->q_gid = 0;
a->q_fullname = NULL;
/*
** Look for owner of alias
*/
else
return;
/* reflect owner into envelope sender */
/* announce delivery to this alias; NORECEIPT bit set later */
"Message delivered to mailing list %s\n",
a->q_paddr);
e->e_flags |= EF_SENDRECEIPT;
}
/*
** ALIASLOOKUP -- look up a name in the alias file.
**
** Parameters:
** name -- the name to look up.
** pstat -- a pointer to a place to put the status.
** av -- argument for %1 expansion.
**
** Returns:
** the value of name.
** NULL if unknown.
**
** Side Effects:
** none.
**
** Warnings:
** The return value will be trashed across calls.
*/
static char *
char *name;
int *pstat;
char *av;
{
int i;
char *argv[4];
#endif /* _FFR_ALIAS_DETAIL */
{
if (s == NULL)
return NULL;
}
/* special case POstMastER -- always use lower case */
name = "postmaster";
i = 0;
/* XXX '+' is hardwired here as delimiter! */
#else /* _FFR_ALIAS_DETAIL */
#endif /* _FFR_ALIAS_DETAIL */
}
/*
** SETALIAS -- set up an alias map
**
** Called when reading configuration file.
**
** Parameters:
** spec -- the alias specification
**
** Returns:
** none.
*/
void
char *spec;
{
register char *p;
char *class;
STAB *s;
{
char buf[50];
p++;
if (*p == '\0')
break;
spec = p;
if (NAliasFileMaps >= MAXMAPSTACK)
{
syserr("Too many alias databases defined, %d max",
return;
}
if (AliasFileMap == NULL)
{
sizeof(buf));
if (AliasFileMap == NULL)
{
syserr("setalias: cannot create aliases.files map");
return;
}
}
p = strpbrk(p, ALIAS_SPEC_SEPARATORS);
{
/* map name */
*p++ = '\0';
spec = p;
}
else
{
class = "implicit";
}
/* find end of spec */
if (p != NULL)
{
bool quoted = false;
for (; *p != '\0'; p++)
{
/*
** Don't break into a quoted string.
** Needed for ldap maps which use
** commas in their specifications.
*/
if (*p == '"')
else if (*p == ',' && !quoted)
break;
}
/* No more alias specifications follow */
if (*p == '\0')
p = NULL;
}
if (p != NULL)
*p++ = '\0';
/* look up class */
if (s == NULL)
{
}
{
syserr("setalias: map class %s can't handle aliases",
class);
}
else
{
{
}
}
}
}
/*
** ALIASWAIT -- wait for distinguished @:@ token to appear.
**
** This can decide to reopen or rebuild the alias file
**
** Parameters:
** map -- a pointer to the map descriptor for this alias file.
** ext -- the filename extension (e.g., ".db") for the
** database file.
** isopen -- if set, the database is already open, and we
** should check for validity; otherwise, we are
** just checking to see if it should be created.
**
** Returns:
** true -- if the database is open when we return.
** false -- if the database is closed when we return.
*/
bool
char *ext;
bool isopen;
{
bool attimeout = false;
char buf[MAXPATHLEN];
sm_dprintf("aliaswait(%s:%s)\n",
return isopen;
if (SafeAlias > 0)
{
auto int st;
unsigned int sleeptime = 2;
unsigned int loopcount = 0; /* only used for debugging */
while (isopen &&
{
{
/* we timed out */
attimeout = true;
break;
}
/*
** Close and re-open the alias database in case
** the one is mv'ed instead of cp'ed in.
*/
{
loopcount++;
sm_dprintf("aliaswait: sleeping for %u seconds (loopcount = %u)\n",
}
sleeptime *= 2;
if (sleeptime > 60)
sleeptime = 60;
}
}
/* see if we need to go into auto-rebuild mode */
{
sm_dprintf("aliaswait: not rebuildable\n");
return isopen;
}
{
sm_dprintf("aliaswait: no source file\n");
return isopen;
}
{
if (LogLevel > 3)
"alias database %s%s name too long",
message("alias database %s%s name too long",
}
{
if (LogLevel > 3)
"alias database %s out of date", buf);
}
return isopen;
}
/*
** REBUILDALIASES -- rebuild the alias database.
**
** Parameters:
** map -- the database to rebuild.
** automatic -- set if this was automatically generated.
**
** Returns:
** true if successful; false otherwise.
**
** Side Effects:
** Reads the text version of the database, builds the
** DBM or DB version.
*/
bool
bool automatic;
{
bool nolock = false;
bool success = false;
#ifdef SIGTSTP
#endif /* SIGTSTP */
return false;
sff |= SFF_NOWLINK;
sff |= SFF_NOGWFILES;
sff |= SFF_NOWWFILES;
/* try to lock the source file */
{
{
sm_dprintf("Can't open %s: %s\n",
message("newaliases: cannot open %s: %s",
errno = 0;
return false;
}
nolock = true;
message("warning: cannot lock %s: %s",
}
/* see if someone else is rebuilding the alias file */
if (!nolock &&
{
/* yes, they are -- wait until done */
message("Alias file %s is locked (maybe being rebuilt)",
if (OpMode != MD_INITALIAS)
{
/* wait for other rebuild to complete */
}
errno = 0;
return false;
}
#ifdef SIGTSTP
#endif /* SIGTSTP */
{
if (LogLevel > 7)
{
"alias database %s %srebuilt by %s",
username());
}
success = true;
}
else
{
sm_dprintf("Can't create database for %s: %s\n",
if (!automatic)
syserr("Cannot create database for alias file %s",
}
/* close the file, thus releasing locks */
/* add distinguished entries and close the database */
{
}
/* restore the old signals */
#ifdef SIGTSTP
#endif /* SIGTSTP */
return success;
}
/*
** READALIASES -- read and process the alias file.
**
** This routine implements the part of initaliases that occurs
** when we are not going to use the DBM stuff.
**
** Parameters:
** map -- the alias database descriptor.
** af -- file to read the aliases from.
** announcestats -- announce statistics regarding number of
** aliases, longest alias, etc.
** logstats -- lot the same info.
**
** Returns:
** none.
**
** Side Effects:
** Reads aliasfile into the symbol table.
** Optionally, builds the .dir & .pag files.
*/
void
bool announcestats;
bool logstats;
{
register char *p;
char *rhs;
bool skipping;
/*
** Read and interpret lines
*/
LineNumber = 0;
skipping = false;
{
int c;
LineNumber++;
/* XXX what if line="a\\" ? */
{
p--;
break;
LineNumber++;
p = strchr(p, '\n');
}
if (p != NULL)
*p = '\0';
{
errno = 0;
syserr("554 5.3.0 alias line too long");
/* flush to end of line */
SM_IO_EOF && c != '\n')
continue;
/* skip any continuation lines */
skipping = true;
continue;
}
switch (line[0])
{
case '#':
case '\0':
skipping = false;
continue;
case ' ':
case '\t':
if (!skipping)
syserr("554 5.3.5 Non-continuation line starts with space");
skipping = true;
continue;
}
skipping = false;
/*
** Process the LHS
** Find the colon separator, and parse the address.
** It should resolve to a local name -- this will
** be checked later (we want to optionally do
** parsing of the RHS first to maximize error
** detection).
*/
continue;
if (*p++ != ':')
{
syserr("554 5.3.5 missing colon");
continue;
}
== NULL)
{
continue;
}
/*
** Process the RHS.
** 'al' is the internal form of the LHS address.
** 'p' points to the text of the RHS.
*/
p++;
rhs = p;
for (;;)
{
register char *nlp;
*--nlp = '\0';
if (CheckAliases)
{
/* do parsing & compression of addresses */
while (*p != '\0')
{
auto char *delimptr;
*p == ',')
p++;
if (*p == '\0')
break;
== NULL)
usrerr("553 5.3.5 %s... bad address", p);
p = delimptr;
}
}
else
{
p = nlp;
}
/* see if there should be a continuation line */
if (c != ' ' && c != '\t')
break;
/* read continuation line */
break;
LineNumber++;
/* check for line overflow */
{
usrerr("554 5.3.5 alias too long");
!= SM_IO_EOF && c != '\n')
continue;
skipping = true;
break;
}
}
if (skipping)
continue;
{
syserr("554 5.3.5 %s... cannot alias non-local names",
continue;
}
/*
** Insert alias into symbol table or database file.
**
** Special case pOStmaStER -- always make it lower case.
*/
if (rhssize > 0)
{
/* is RHS empty (just spaces)? */
p = rhs;
p++;
}
if (rhssize == 0 || *p == '\0')
{
syserr("554 5.3.5 %.40s... missing value for alias",
line);
}
else
{
/* statistics */
naliases++;
}
#if 0
/*
** address strings are now stored in the envelope rpool,
** and therefore cannot be freed.
*/
#endif /* 0 */
}
if (Verbose || announcestats)
message("%s: %ld aliases, longest %ld bytes, %ld bytes total",
"%s: %ld aliases, longest %ld bytes, %ld bytes total",
}
/*
** FORWARD -- Try to forward mail
**
** This is similar but not identical to aliasing.
**
** Parameters:
** user -- the name of the user who's mail we would like
** to forward to. It must have been verified --
** i.e., the q_home field must have been filled
** in.
** sendq -- a pointer to the head of the send queue to
** put this user's aliases in.
** aliaslevel -- the current alias nesting depth.
** e -- the current envelope.
**
** Returns:
** none.
**
** Side Effects:
** New names are added to send queues.
*/
void
int aliaslevel;
register ENVELOPE *e;
{
char *pp;
char *ep;
bool got_transient;
return;
return;
{
syserr("554 5.3.0 forward: no home");
}
/* good address -- look for .forward file in home */
if (ForwardPath == NULL)
got_transient = false;
{
int err;
char buf[MAXPATHLEN];
*ep = '\0';
if (buf[0] == '\0')
continue;
if (err == 0)
break;
else if (transienterror(err))
{
/* we may have to suspend this message */
got_transient = true;
sm_dprintf("forward: transient error on %s\n",
buf);
if (LogLevel > 2)
{
char *curhost = CurHostName;
CurHostName = NULL;
"forward %s: transient error: %s",
}
}
else
{
switch (err)
{
case ENOENT:
break;
case E_SM_WWDIR:
case E_SM_GWDIR:
/* check if it even exists */
{
break;
}
/* FALLTHROUGH */
case E_SM_NOSLINK:
case E_SM_NOHLINK:
case E_SM_REGONLY:
case E_SM_ISEXEC:
case E_SM_WWFILE:
case E_SM_GWFILE:
break;
#endif /* _FFR_FORWARD_SYSERR */
default:
"forward %s: %s", buf,
sm_errstring(err));
if (Verbose)
message("forward: %s: %s",
break;
}
}
}
{
/*
** There was no successful .forward open and at least one
** transient open. We have to defer this address for
** further delivery.
*/
message("transient .forward open error: message queued");
return;
}
}