1998-08-03 05:56:20 +00:00
|
|
|
/*
|
2001-02-28 00:22:47 +00:00
|
|
|
* Copyright (c) 1998-2001 Sendmail, Inc. and its suppliers.
|
2000-08-12 22:19:16 +00:00
|
|
|
* All rights reserved.
|
1998-08-03 05:56:20 +00:00
|
|
|
* Copyright (c) 1983 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
#include <sm/gen.h>
|
|
|
|
|
|
|
|
SM_IDSTR(copyright,
|
2001-02-28 00:22:47 +00:00
|
|
|
"@(#) Copyright (c) 1998-2001 Sendmail, Inc. and its suppliers.\n\
|
2000-08-12 22:19:16 +00:00
|
|
|
All rights reserved.\n\
|
|
|
|
Copyright (c) 1983 Eric P. Allman. All rights reserved.\n\
|
|
|
|
Copyright (c) 1988, 1993\n\
|
2002-02-17 21:58:34 +00:00
|
|
|
The Regents of the University of California. All rights reserved.\n")
|
1998-08-03 05:56:20 +00:00
|
|
|
|
2002-06-11 21:16:51 +00:00
|
|
|
SM_IDSTR(id, "@(#)$Id: praliases.c,v 8.93 2001/09/11 04:05:07 gshapiro Exp $")
|
2000-08-12 22:19:16 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#ifdef EX_OK
|
|
|
|
# undef EX_OK /* unistd.h may have another use for this */
|
|
|
|
#endif /* EX_OK */
|
|
|
|
#include <sysexits.h>
|
1998-08-03 05:56:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
#ifndef NOT_SENDMAIL
|
|
|
|
# define NOT_SENDMAIL
|
2000-08-12 22:19:16 +00:00
|
|
|
#endif /* ! NOT_SENDMAIL */
|
|
|
|
#include <sendmail/sendmail.h>
|
|
|
|
#include <sendmail/pathnames.h>
|
|
|
|
#include <libsmdb/smdb.h>
|
1998-08-03 05:56:20 +00:00
|
|
|
|
1999-01-12 12:38:06 +00:00
|
|
|
static void praliases __P((char *, int, char **));
|
2000-08-12 22:19:16 +00:00
|
|
|
|
|
|
|
uid_t RealUid;
|
|
|
|
gid_t RealGid;
|
|
|
|
char *RealUserName;
|
|
|
|
uid_t RunAsUid;
|
|
|
|
uid_t RunAsGid;
|
|
|
|
char *RunAsUserName;
|
|
|
|
int Verbose = 2;
|
2002-02-17 21:58:34 +00:00
|
|
|
bool DontInitGroups = false;
|
2000-08-12 22:19:16 +00:00
|
|
|
uid_t TrustedUid = 0;
|
|
|
|
BITMAP256 DontBlameSendmail;
|
|
|
|
|
2001-01-21 22:21:43 +00:00
|
|
|
# define DELIMITERS " ,/"
|
|
|
|
# define PATH_SEPARATOR ':'
|
1999-01-12 12:38:06 +00:00
|
|
|
|
1998-08-03 05:56:20 +00:00
|
|
|
int
|
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char **argv;
|
|
|
|
{
|
1999-01-12 12:38:06 +00:00
|
|
|
char *cfile;
|
|
|
|
char *filename = NULL;
|
2002-02-17 21:58:34 +00:00
|
|
|
SM_FILE_T *cfp;
|
1998-08-03 05:56:20 +00:00
|
|
|
int ch;
|
1999-01-12 12:38:06 +00:00
|
|
|
char afilebuf[MAXLINE];
|
|
|
|
char buf[MAXLINE];
|
2000-08-12 22:19:16 +00:00
|
|
|
struct passwd *pw;
|
|
|
|
static char rnamebuf[MAXNAME];
|
|
|
|
extern char *optarg;
|
|
|
|
extern int optind;
|
|
|
|
|
|
|
|
clrbitmap(DontBlameSendmail);
|
|
|
|
RunAsUid = RealUid = getuid();
|
|
|
|
RunAsGid = RealGid = getgid();
|
|
|
|
pw = getpwuid(RealUid);
|
|
|
|
if (pw != NULL)
|
|
|
|
{
|
|
|
|
if (strlen(pw->pw_name) > MAXNAME - 1)
|
|
|
|
pw->pw_name[MAXNAME] = 0;
|
2002-02-17 21:58:34 +00:00
|
|
|
sm_snprintf(rnamebuf, sizeof rnamebuf, "%s", pw->pw_name);
|
2000-08-12 22:19:16 +00:00
|
|
|
}
|
|
|
|
else
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_snprintf(rnamebuf, sizeof rnamebuf,
|
|
|
|
"Unknown UID %d", (int) RealUid);
|
2000-08-12 22:19:16 +00:00
|
|
|
RunAsUserName = RealUserName = rnamebuf;
|
1998-08-03 05:56:20 +00:00
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
cfile = getcfname(0, 0, SM_GET_SENDMAIL_CF, NULL);
|
1999-01-12 12:38:06 +00:00
|
|
|
while ((ch = getopt(argc, argv, "C:f:")) != -1)
|
|
|
|
{
|
|
|
|
switch ((char)ch) {
|
|
|
|
case 'C':
|
|
|
|
cfile = optarg;
|
|
|
|
break;
|
1998-08-03 05:56:20 +00:00
|
|
|
case 'f':
|
|
|
|
filename = optarg;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
|
|
|
|
"usage: praliases [-C cffile] [-f aliasfile]\n");
|
1998-08-03 05:56:20 +00:00
|
|
|
exit(EX_USAGE);
|
|
|
|
}
|
1999-01-12 12:38:06 +00:00
|
|
|
}
|
1998-08-03 05:56:20 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
1999-01-12 12:38:06 +00:00
|
|
|
if (filename != NULL)
|
|
|
|
{
|
|
|
|
praliases(filename, argc, argv);
|
|
|
|
exit(EX_OK);
|
|
|
|
}
|
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
if ((cfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, cfile, SM_IO_RDONLY,
|
|
|
|
NULL)) == NULL)
|
1999-01-12 12:38:06 +00:00
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
|
|
|
|
"praliases: %s: %s\n", cfile,
|
|
|
|
sm_errstring(errno));
|
1999-01-12 12:38:06 +00:00
|
|
|
exit(EX_NOINPUT);
|
|
|
|
}
|
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
while (sm_io_fgets(cfp, SM_TIME_DEFAULT, buf, sizeof(buf)) != NULL)
|
1999-01-12 12:38:06 +00:00
|
|
|
{
|
|
|
|
register char *b, *p;
|
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
b = strchr(buf, '\n');
|
|
|
|
if (b != NULL)
|
|
|
|
*b = '\0';
|
|
|
|
|
1999-01-12 12:38:06 +00:00
|
|
|
b = buf;
|
|
|
|
switch (*b++)
|
|
|
|
{
|
|
|
|
case 'O': /* option -- see if alias file */
|
2002-02-17 21:58:34 +00:00
|
|
|
if (sm_strncasecmp(b, " AliasFile", 10) == 0 &&
|
1999-01-12 12:38:06 +00:00
|
|
|
!(isascii(b[10]) && isalnum(b[10])))
|
|
|
|
{
|
|
|
|
/* new form -- find value */
|
|
|
|
b = strchr(b, '=');
|
|
|
|
if (b == NULL)
|
|
|
|
continue;
|
|
|
|
while (isascii(*++b) && isspace(*b))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (*b++ != 'A')
|
|
|
|
{
|
|
|
|
/* something else boring */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this is the A or AliasFile option -- save it */
|
2002-02-17 21:58:34 +00:00
|
|
|
if (sm_strlcpy(afilebuf, b, sizeof afilebuf) >=
|
2000-08-12 22:19:16 +00:00
|
|
|
sizeof afilebuf)
|
1999-01-12 12:38:06 +00:00
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
|
|
|
|
"praliases: AliasFile filename too long: %.30s\n",
|
1999-01-12 12:38:06 +00:00
|
|
|
b);
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_io_close(cfp, SM_TIME_DEFAULT);
|
1999-01-12 12:38:06 +00:00
|
|
|
exit(EX_CONFIG);
|
|
|
|
}
|
|
|
|
b = afilebuf;
|
|
|
|
|
|
|
|
for (p = b; p != NULL; )
|
|
|
|
{
|
|
|
|
while (isascii(*p) && isspace(*p))
|
|
|
|
p++;
|
|
|
|
if (*p == '\0')
|
|
|
|
break;
|
|
|
|
b = p;
|
|
|
|
|
2001-01-21 22:21:43 +00:00
|
|
|
p = strpbrk(p, DELIMITERS);
|
2000-08-12 22:19:16 +00:00
|
|
|
|
1999-01-12 12:38:06 +00:00
|
|
|
/* find end of spec */
|
|
|
|
if (p != NULL)
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
bool quoted = false;
|
2000-08-12 22:19:16 +00:00
|
|
|
|
|
|
|
for (; *p != '\0'; p++)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
** Don't break into a quoted
|
|
|
|
** string.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (*p == '"')
|
|
|
|
quoted = !quoted;
|
|
|
|
else if (*p == ',' && !quoted)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No more alias specs follow */
|
|
|
|
if (*p == '\0')
|
|
|
|
{
|
|
|
|
/* chop trailing whitespace */
|
|
|
|
while (isascii(*p) &&
|
|
|
|
isspace(*p) &&
|
|
|
|
p > b)
|
|
|
|
p--;
|
|
|
|
*p = '\0';
|
|
|
|
p = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-12 12:38:06 +00:00
|
|
|
if (p != NULL)
|
2000-08-12 22:19:16 +00:00
|
|
|
{
|
|
|
|
char *e = p - 1;
|
1999-01-12 12:38:06 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
/* chop trailing whitespace */
|
|
|
|
while (isascii(*e) &&
|
|
|
|
isspace(*e) &&
|
|
|
|
e > b)
|
|
|
|
e--;
|
|
|
|
*++e = '\0';
|
|
|
|
*p++ = '\0';
|
|
|
|
}
|
1999-01-12 12:38:06 +00:00
|
|
|
praliases(b, argc, argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_io_close(cfp, SM_TIME_DEFAULT);
|
1999-01-12 12:38:06 +00:00
|
|
|
exit(EX_OK);
|
2000-08-12 22:19:16 +00:00
|
|
|
/* NOTREACHED */
|
|
|
|
return EX_OK;
|
1999-01-12 12:38:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
praliases(filename, argc, argv)
|
|
|
|
char *filename;
|
2000-08-12 22:19:16 +00:00
|
|
|
int argc;
|
1999-01-12 12:38:06 +00:00
|
|
|
char **argv;
|
|
|
|
{
|
2000-08-12 22:19:16 +00:00
|
|
|
int result;
|
|
|
|
char *colon;
|
|
|
|
char *db_name;
|
|
|
|
char *db_type;
|
|
|
|
SMDB_DATABASE *database = NULL;
|
|
|
|
SMDB_CURSOR *cursor = NULL;
|
|
|
|
SMDB_DBENT db_key, db_value;
|
|
|
|
SMDB_DBPARAMS params;
|
|
|
|
SMDB_USER_INFO user_info;
|
|
|
|
|
2001-01-21 22:21:43 +00:00
|
|
|
colon = strchr(filename, PATH_SEPARATOR);
|
2000-08-12 22:19:16 +00:00
|
|
|
if (colon == NULL)
|
1999-01-12 12:38:06 +00:00
|
|
|
{
|
2000-08-12 22:19:16 +00:00
|
|
|
db_name = filename;
|
|
|
|
db_type = SMDB_TYPE_DEFAULT;
|
1999-01-12 12:38:06 +00:00
|
|
|
}
|
2000-08-12 22:19:16 +00:00
|
|
|
else
|
1998-08-03 05:56:20 +00:00
|
|
|
{
|
2000-08-12 22:19:16 +00:00
|
|
|
*colon = '\0';
|
|
|
|
db_name = colon + 1;
|
|
|
|
db_type = filename;
|
1998-08-03 05:56:20 +00:00
|
|
|
}
|
2000-08-12 22:19:16 +00:00
|
|
|
|
|
|
|
/* clean off arguments */
|
|
|
|
for (;;)
|
1998-08-03 05:56:20 +00:00
|
|
|
{
|
2000-08-12 22:19:16 +00:00
|
|
|
while (isascii(*db_name) && isspace(*db_name))
|
|
|
|
db_name++;
|
2001-01-21 22:21:43 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
if (*db_name != '-')
|
|
|
|
break;
|
|
|
|
while (*db_name != '\0' &&
|
|
|
|
!(isascii(*db_name) && isspace(*db_name)))
|
|
|
|
db_name++;
|
1998-08-03 05:56:20 +00:00
|
|
|
}
|
2000-08-12 22:19:16 +00:00
|
|
|
|
2001-02-28 00:22:47 +00:00
|
|
|
/* Skip non-file based DB types */
|
|
|
|
if (db_type != NULL && *db_type != '\0')
|
|
|
|
{
|
|
|
|
if (db_type != SMDB_TYPE_DEFAULT &&
|
|
|
|
strcmp(db_type, "hash") != 0 &&
|
|
|
|
strcmp(db_type, "btree") != 0 &&
|
|
|
|
strcmp(db_type, "dbm") != 0)
|
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
|
|
|
|
"praliases: Skipping non-file based alias type %s\n",
|
2001-02-28 00:22:47 +00:00
|
|
|
db_type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
if (*db_name == '\0' || (db_type != NULL && *db_type == '\0'))
|
1999-01-12 12:38:06 +00:00
|
|
|
{
|
2000-08-12 22:19:16 +00:00
|
|
|
if (colon != NULL)
|
|
|
|
*colon = ':';
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
|
|
|
|
"praliases: illegal alias specification: %s\n", filename);
|
2000-08-12 22:19:16 +00:00
|
|
|
goto fatal;
|
1999-01-12 12:38:06 +00:00
|
|
|
}
|
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
memset(¶ms, '\0', sizeof params);
|
|
|
|
params.smdbp_cache_size = 1024 * 1024;
|
1999-01-12 12:38:06 +00:00
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
user_info.smdbu_id = RunAsUid;
|
|
|
|
user_info.smdbu_group_id = RunAsGid;
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_strlcpy(user_info.smdbu_name, RunAsUserName,
|
|
|
|
SMDB_MAX_USER_NAME_LEN);
|
2000-08-12 22:19:16 +00:00
|
|
|
|
|
|
|
result = smdb_open_database(&database, db_name, O_RDONLY, 0,
|
|
|
|
SFF_ROOTOK, db_type, &user_info, ¶ms);
|
|
|
|
if (result != SMDBE_OK)
|
1999-01-12 12:38:06 +00:00
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
|
|
|
|
"praliases: %s: open: %s\n",
|
|
|
|
db_name, sm_errstring(result));
|
2000-08-12 22:19:16 +00:00
|
|
|
goto fatal;
|
1999-01-12 12:38:06 +00:00
|
|
|
}
|
2000-08-12 22:19:16 +00:00
|
|
|
|
|
|
|
if (argc == 0)
|
1999-01-12 12:38:06 +00:00
|
|
|
{
|
2000-08-12 22:19:16 +00:00
|
|
|
memset(&db_key, '\0', sizeof db_key);
|
|
|
|
memset(&db_value, '\0', sizeof db_value);
|
|
|
|
|
|
|
|
result = database->smdb_cursor(database, &cursor, 0);
|
|
|
|
if (result != SMDBE_OK)
|
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
|
|
|
|
"praliases: %s: set cursor: %s\n", db_name,
|
|
|
|
sm_errstring(result));
|
2000-08-12 22:19:16 +00:00
|
|
|
goto fatal;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((result = cursor->smdbc_get(cursor, &db_key, &db_value,
|
|
|
|
SMDB_CURSOR_GET_NEXT)) ==
|
|
|
|
SMDBE_OK)
|
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
/* skip magic @:@ entry */
|
2001-01-21 22:21:43 +00:00
|
|
|
if (db_key.size == 2 &&
|
|
|
|
db_key.data[0] == '@' &&
|
|
|
|
db_key.data[1] == '\0' &&
|
|
|
|
db_value.size == 2 &&
|
|
|
|
db_value.data[0] == '@' &&
|
|
|
|
db_value.data[1] == '\0')
|
2000-08-12 22:19:16 +00:00
|
|
|
continue;
|
|
|
|
#endif /* 0 */
|
|
|
|
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
|
|
|
|
"%.*s:%.*s\n",
|
|
|
|
(int) db_key.size,
|
|
|
|
(char *) db_key.data,
|
|
|
|
(int) db_value.size,
|
|
|
|
(char *) db_value.data);
|
2000-08-12 22:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (result != SMDBE_OK && result != SMDBE_LAST_ENTRY)
|
1999-01-12 12:38:06 +00:00
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
|
2000-08-12 22:19:16 +00:00
|
|
|
"praliases: %s: get value at cursor: %s\n",
|
2002-02-17 21:58:34 +00:00
|
|
|
db_name, sm_errstring(result));
|
2000-08-12 22:19:16 +00:00
|
|
|
goto fatal;
|
1998-08-03 05:56:20 +00:00
|
|
|
}
|
1999-01-12 12:38:06 +00:00
|
|
|
}
|
2000-08-12 22:19:16 +00:00
|
|
|
else for (; *argv != NULL; ++argv)
|
1999-01-12 12:38:06 +00:00
|
|
|
{
|
2001-05-28 17:10:35 +00:00
|
|
|
int get_res;
|
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
memset(&db_key, '\0', sizeof db_key);
|
|
|
|
memset(&db_value, '\0', sizeof db_value);
|
2001-01-21 22:21:43 +00:00
|
|
|
db_key.data = *argv;
|
2001-05-28 17:10:35 +00:00
|
|
|
db_key.size = strlen(*argv);
|
|
|
|
get_res = database->smdb_get(database, &db_key, &db_value, 0);
|
|
|
|
if (get_res == SMDBE_NOT_FOUND)
|
|
|
|
{
|
|
|
|
db_key.size++;
|
|
|
|
get_res = database->smdb_get(database, &db_key,
|
|
|
|
&db_value, 0);
|
|
|
|
}
|
|
|
|
if (get_res == SMDBE_OK)
|
1999-01-12 12:38:06 +00:00
|
|
|
{
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
|
|
|
|
"%.*s:%.*s\n",
|
|
|
|
(int) db_key.size,
|
|
|
|
(char *) db_key.data,
|
|
|
|
(int) db_value.size,
|
|
|
|
(char *) db_value.data);
|
1998-08-03 05:56:20 +00:00
|
|
|
}
|
2000-08-12 22:19:16 +00:00
|
|
|
else
|
2002-02-17 21:58:34 +00:00
|
|
|
(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
|
|
|
|
"%s: No such key\n",
|
|
|
|
(char *)db_key.data);
|
1998-08-03 05:56:20 +00:00
|
|
|
}
|
|
|
|
|
2000-08-12 22:19:16 +00:00
|
|
|
fatal:
|
|
|
|
if (cursor != NULL)
|
|
|
|
(void) cursor->smdbc_close(cursor);
|
|
|
|
if (database != NULL)
|
|
|
|
(void) database->smdb_close(database);
|
|
|
|
if (colon != NULL)
|
|
|
|
*colon = ':';
|
|
|
|
return;
|
|
|
|
}
|