1998-08-03 05:56:20 +00:00
|
|
|
/*
|
2001-05-28 17:08:52 +00:00
|
|
|
* Copyright (c) 1998-2001 Sendmail, Inc. and its suppliers.
|
2000-08-12 21:55:49 +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:56:45 +00:00
|
|
|
#include <sm/gen.h>
|
|
|
|
|
|
|
|
SM_IDSTR(copyright,
|
2001-05-28 17:08:52 +00:00
|
|
|
"@(#) Copyright (c) 1998-2001 Sendmail, Inc. and its suppliers.\n\
|
2000-08-12 21:55:49 +00:00
|
|
|
All rights reserved.\n\
|
|
|
|
Copyright (c) 1988, 1993\n\
|
2002-02-17 21:56:45 +00:00
|
|
|
The Regents of the University of California. All rights reserved.\n")
|
1998-08-03 05:56:20 +00:00
|
|
|
|
2002-04-20 20:31:51 +00:00
|
|
|
SM_IDSTR(id, "@(#)$Id: mailstats.c,v 1.1.1.8 2002/02/17 21:56:43 gshapiro Exp $")
|
2000-08-12 21:55:49 +00:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#ifdef EX_OK
|
|
|
|
# undef EX_OK /* unistd.h may have another use for this */
|
|
|
|
#endif /* EX_OK */
|
|
|
|
#include <sysexits.h>
|
|
|
|
|
2002-02-17 21:56:45 +00:00
|
|
|
#include <sm/errstring.h>
|
|
|
|
#include <sm/limits.h>
|
2000-08-12 21:55:49 +00:00
|
|
|
#include <sendmail/sendmail.h>
|
|
|
|
#include <sendmail/mailstats.h>
|
|
|
|
#include <sendmail/pathnames.h>
|
1998-08-03 05:56:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
#define MNAMELEN 20 /* max length of mailer name */
|
|
|
|
|
|
|
|
int
|
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char **argv;
|
|
|
|
{
|
|
|
|
register int i;
|
|
|
|
int mno;
|
2000-08-12 21:55:49 +00:00
|
|
|
int save_errno;
|
1998-08-03 05:56:20 +00:00
|
|
|
int ch, fd;
|
|
|
|
char *sfile;
|
|
|
|
char *cfile;
|
2002-02-17 21:56:45 +00:00
|
|
|
SM_FILE_T *cfp;
|
1998-08-03 05:56:20 +00:00
|
|
|
bool mnames;
|
1999-01-12 12:26:57 +00:00
|
|
|
bool progmode;
|
2002-02-17 21:56:45 +00:00
|
|
|
bool trunc;
|
1998-08-03 05:56:20 +00:00
|
|
|
long frmsgs = 0, frbytes = 0, tomsgs = 0, tobytes = 0, rejmsgs = 0;
|
|
|
|
long dismsgs = 0;
|
2002-02-17 21:56:45 +00:00
|
|
|
#if _FFR_QUARANTINE
|
|
|
|
long quarmsgs = 0;
|
|
|
|
#endif /* _FFR_QUARANTINE */
|
2000-08-12 21:55:49 +00:00
|
|
|
time_t now;
|
|
|
|
char mtable[MAXMAILERS][MNAMELEN + 1];
|
1998-08-03 05:56:20 +00:00
|
|
|
char sfilebuf[MAXLINE];
|
|
|
|
char buf[MAXLINE];
|
2000-08-12 21:55:49 +00:00
|
|
|
struct statistics stats;
|
1998-08-03 05:56:20 +00:00
|
|
|
extern char *ctime();
|
2000-08-12 21:55:49 +00:00
|
|
|
extern char *optarg;
|
|
|
|
extern int optind;
|
|
|
|
|
2002-02-17 21:56:45 +00:00
|
|
|
cfile = getcfname(0, 0, SM_GET_SENDMAIL_CF, NULL);
|
1998-08-03 05:56:20 +00:00
|
|
|
sfile = NULL;
|
2002-02-17 21:56:45 +00:00
|
|
|
mnames = true;
|
|
|
|
progmode = false;
|
|
|
|
trunc = false;
|
|
|
|
while ((ch = getopt(argc, argv, "cC:f:opP")) != -1)
|
1998-08-03 05:56:20 +00:00
|
|
|
{
|
|
|
|
switch (ch)
|
|
|
|
{
|
2002-02-17 21:56:45 +00:00
|
|
|
case 'c':
|
|
|
|
cfile = getcfname(0, 0, SM_GET_SUBMIT_CF, NULL);
|
|
|
|
break;
|
|
|
|
|
1998-08-03 05:56:20 +00:00
|
|
|
case 'C':
|
|
|
|
cfile = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
sfile = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'o':
|
2002-02-17 21:56:45 +00:00
|
|
|
mnames = false;
|
1998-08-03 05:56:20 +00:00
|
|
|
break;
|
|
|
|
|
1999-01-12 12:26:57 +00:00
|
|
|
case 'p':
|
2002-02-17 21:56:45 +00:00
|
|
|
trunc = true;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
|
|
|
|
case 'P':
|
|
|
|
progmode = true;
|
1999-01-12 12:26:57 +00:00
|
|
|
break;
|
|
|
|
|
1998-08-03 05:56:20 +00:00
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage:
|
2002-02-17 21:56:45 +00:00
|
|
|
(void) sm_io_fputs(smioerr, SM_TIME_DEFAULT,
|
|
|
|
"usage: mailstats [-C cffile] [-P] [-f stfile] [-o] [-p]\n");
|
1998-08-03 05:56:20 +00:00
|
|
|
exit(EX_USAGE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc != 0)
|
|
|
|
goto usage;
|
|
|
|
|
2002-02-17 21:56:45 +00:00
|
|
|
if ((cfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, cfile, SM_IO_RDONLY,
|
|
|
|
NULL)) == NULL)
|
1998-08-03 05:56:20 +00:00
|
|
|
{
|
2000-08-12 21:55:49 +00:00
|
|
|
save_errno = errno;
|
2002-02-17 21:56:45 +00:00
|
|
|
(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT, "mailstats: ");
|
2000-08-12 21:55:49 +00:00
|
|
|
errno = save_errno;
|
2002-02-17 21:56:45 +00:00
|
|
|
sm_perror(cfile);
|
1998-08-03 05:56:20 +00:00
|
|
|
exit(EX_NOINPUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
mno = 0;
|
2002-02-17 21:56:45 +00:00
|
|
|
(void) sm_strlcpy(mtable[mno++], "prog", MNAMELEN + 1);
|
|
|
|
(void) sm_strlcpy(mtable[mno++], "*file*", MNAMELEN + 1);
|
|
|
|
(void) sm_strlcpy(mtable[mno++], "*include*", MNAMELEN + 1);
|
1998-08-03 05:56:20 +00:00
|
|
|
|
2002-02-17 21:56:45 +00:00
|
|
|
while (sm_io_fgets(cfp, SM_TIME_DEFAULT, buf, sizeof(buf)) != NULL)
|
1998-08-03 05:56:20 +00:00
|
|
|
{
|
|
|
|
register char *b;
|
|
|
|
char *s;
|
|
|
|
register char *m;
|
|
|
|
|
|
|
|
b = buf;
|
|
|
|
switch (*b++)
|
|
|
|
{
|
|
|
|
case 'M': /* mailer definition */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'O': /* option -- see if .st file */
|
2002-02-17 21:56:45 +00:00
|
|
|
if (sm_strncasecmp(b, " StatusFile", 11) == 0 &&
|
1998-08-03 05:56:20 +00:00
|
|
|
!(isascii(b[11]) && isalnum(b[11])))
|
|
|
|
{
|
|
|
|
/* new form -- find value */
|
|
|
|
b = strchr(b, '=');
|
|
|
|
if (b == NULL)
|
|
|
|
continue;
|
|
|
|
while (isascii(*++b) && isspace(*b))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (*b++ != 'S')
|
|
|
|
{
|
|
|
|
/* something else boring */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this is the S or StatusFile option -- save it */
|
2002-02-17 21:56:45 +00:00
|
|
|
if (sm_strlcpy(sfilebuf, b, sizeof sfilebuf) >=
|
2000-08-12 21:55:49 +00:00
|
|
|
sizeof sfilebuf)
|
1998-08-03 05:56:20 +00:00
|
|
|
{
|
2002-02-17 21:56:45 +00:00
|
|
|
(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
|
|
|
|
"StatusFile filename too long: %.30s...\n",
|
|
|
|
b);
|
1998-08-03 05:56:20 +00:00
|
|
|
exit(EX_CONFIG);
|
|
|
|
}
|
|
|
|
b = strchr(sfilebuf, '#');
|
|
|
|
if (b == NULL)
|
|
|
|
b = strchr(sfilebuf, '\n');
|
|
|
|
if (b == NULL)
|
|
|
|
b = &sfilebuf[strlen(sfilebuf)];
|
|
|
|
while (isascii(*--b) && isspace(*b))
|
|
|
|
continue;
|
|
|
|
*++b = '\0';
|
|
|
|
if (sfile == NULL)
|
|
|
|
sfile = sfilebuf;
|
|
|
|
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mno >= MAXMAILERS)
|
|
|
|
{
|
2002-02-17 21:56:45 +00:00
|
|
|
(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
|
|
|
|
"Too many mailers defined, %d max.\n",
|
|
|
|
MAXMAILERS);
|
1998-08-03 05:56:20 +00:00
|
|
|
exit(EX_SOFTWARE);
|
|
|
|
}
|
|
|
|
m = mtable[mno];
|
2000-08-12 21:55:49 +00:00
|
|
|
s = m + MNAMELEN; /* is [MNAMELEN + 1] */
|
1998-08-03 05:56:20 +00:00
|
|
|
while (*b != ',' && !(isascii(*b) && isspace(*b)) &&
|
|
|
|
*b != '\0' && m < s)
|
|
|
|
*m++ = *b++;
|
|
|
|
*m = '\0';
|
|
|
|
for (i = 0; i < mno; i++)
|
|
|
|
{
|
|
|
|
if (strcmp(mtable[i], mtable[mno]) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i == mno)
|
|
|
|
mno++;
|
|
|
|
}
|
2002-02-17 21:56:45 +00:00
|
|
|
(void) sm_io_close(cfp, SM_TIME_DEFAULT);
|
1998-08-03 05:56:20 +00:00
|
|
|
for (; mno < MAXMAILERS; mno++)
|
2002-02-17 21:56:45 +00:00
|
|
|
mtable[mno][0] = '\0';
|
1998-08-03 05:56:20 +00:00
|
|
|
|
|
|
|
if (sfile == NULL)
|
|
|
|
{
|
2002-02-17 21:56:45 +00:00
|
|
|
(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
|
|
|
|
"mailstats: no statistics file located\n");
|
1998-08-03 05:56:20 +00:00
|
|
|
exit (EX_OSFILE);
|
|
|
|
}
|
|
|
|
|
2000-08-12 21:55:49 +00:00
|
|
|
fd = open(sfile, O_RDONLY);
|
|
|
|
if ((fd < 0) || (i = read(fd, &stats, sizeof stats)) < 0)
|
1998-08-03 05:56:20 +00:00
|
|
|
{
|
2000-08-12 21:55:49 +00:00
|
|
|
save_errno = errno;
|
2002-02-17 21:56:45 +00:00
|
|
|
(void) sm_io_fputs(smioerr, SM_TIME_DEFAULT, "mailstats: ");
|
2000-08-12 21:55:49 +00:00
|
|
|
errno = save_errno;
|
2002-02-17 21:56:45 +00:00
|
|
|
sm_perror(sfile);
|
1998-08-03 05:56:20 +00:00
|
|
|
exit(EX_NOINPUT);
|
|
|
|
}
|
|
|
|
if (i == 0)
|
|
|
|
{
|
2000-08-12 21:55:49 +00:00
|
|
|
(void) sleep(1);
|
|
|
|
if ((i = read(fd, &stats, sizeof stats)) < 0)
|
1998-08-03 05:56:20 +00:00
|
|
|
{
|
2000-08-12 21:55:49 +00:00
|
|
|
save_errno = errno;
|
2002-02-17 21:56:45 +00:00
|
|
|
(void) sm_io_fputs(smioerr, SM_TIME_DEFAULT,
|
|
|
|
"mailstats: ");
|
2000-08-12 21:55:49 +00:00
|
|
|
errno = save_errno;
|
2002-02-17 21:56:45 +00:00
|
|
|
sm_perror(sfile);
|
1998-08-03 05:56:20 +00:00
|
|
|
exit(EX_NOINPUT);
|
|
|
|
}
|
|
|
|
else if (i == 0)
|
|
|
|
{
|
2000-08-12 21:55:49 +00:00
|
|
|
memset((ARBPTR_T) &stats, '\0', sizeof stats);
|
|
|
|
(void) time(&stats.stat_itime);
|
1998-08-03 05:56:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i != 0)
|
|
|
|
{
|
2000-08-12 21:55:49 +00:00
|
|
|
if (stats.stat_magic != STAT_MAGIC)
|
1998-08-03 05:56:20 +00:00
|
|
|
{
|
2002-02-17 21:56:45 +00:00
|
|
|
(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
|
|
|
|
"mailstats: incorrect magic number in %s\n",
|
|
|
|
sfile);
|
1998-08-03 05:56:20 +00:00
|
|
|
exit(EX_OSERR);
|
|
|
|
}
|
2000-08-12 21:55:49 +00:00
|
|
|
else if (stats.stat_version != STAT_VERSION)
|
1998-08-03 05:56:20 +00:00
|
|
|
{
|
2002-02-17 21:56:45 +00:00
|
|
|
(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
|
|
|
|
"mailstats version (%d) incompatible with %s version (%d)\n",
|
|
|
|
STAT_VERSION, sfile,
|
|
|
|
stats.stat_version);
|
|
|
|
|
1998-08-03 05:56:20 +00:00
|
|
|
exit(EX_OSERR);
|
|
|
|
}
|
2000-08-12 21:55:49 +00:00
|
|
|
else if (i != sizeof stats || stats.stat_size != sizeof(stats))
|
1998-08-03 05:56:20 +00:00
|
|
|
{
|
2002-02-17 21:56:45 +00:00
|
|
|
(void) sm_io_fputs(smioerr, SM_TIME_DEFAULT,
|
|
|
|
"mailstats: file size changed.\n");
|
1998-08-03 05:56:20 +00:00
|
|
|
exit(EX_OSERR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-12 12:26:57 +00:00
|
|
|
if (progmode)
|
|
|
|
{
|
2000-08-12 21:55:49 +00:00
|
|
|
(void) time(&now);
|
2002-02-17 21:56:45 +00:00
|
|
|
(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "%ld %ld\n",
|
|
|
|
(long) stats.stat_itime, (long) now);
|
1999-01-12 12:26:57 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-02-17 21:56:45 +00:00
|
|
|
(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
|
|
|
|
"Statistics from %s",
|
|
|
|
ctime(&stats.stat_itime));
|
|
|
|
(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
|
|
|
|
" M msgsfr bytes_from msgsto bytes_to msgsrej msgsdis");
|
|
|
|
#if _FFR_QUARANTINE
|
|
|
|
(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, " msgsqur");
|
|
|
|
#endif /* _FFR_QUARANTINE */
|
|
|
|
(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "%s\n",
|
|
|
|
mnames ? " Mailer" : "");
|
1999-01-12 12:26:57 +00:00
|
|
|
}
|
1998-08-03 05:56:20 +00:00
|
|
|
for (i = 0; i < MAXMAILERS; i++)
|
|
|
|
{
|
2000-08-12 21:55:49 +00:00
|
|
|
if (stats.stat_nf[i] || stats.stat_nt[i] ||
|
2002-02-17 21:56:45 +00:00
|
|
|
#if _FFR_QUARANTINE
|
|
|
|
stats.stat_nq[i] ||
|
|
|
|
#endif /* _FFR_QUARANTINE */
|
2000-08-12 21:55:49 +00:00
|
|
|
stats.stat_nr[i] || stats.stat_nd[i])
|
1998-08-03 05:56:20 +00:00
|
|
|
{
|
1999-01-12 12:26:57 +00:00
|
|
|
char *format;
|
|
|
|
|
|
|
|
if (progmode)
|
|
|
|
format = "%2d %8ld %10ld %8ld %10ld %6ld %6ld";
|
|
|
|
else
|
|
|
|
format = "%2d %8ld %10ldK %8ld %10ldK %6ld %6ld";
|
2002-02-17 21:56:45 +00:00
|
|
|
(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
|
|
|
|
format, i,
|
|
|
|
stats.stat_nf[i],
|
|
|
|
stats.stat_bf[i],
|
|
|
|
stats.stat_nt[i],
|
|
|
|
stats.stat_bt[i],
|
|
|
|
stats.stat_nr[i],
|
|
|
|
stats.stat_nd[i]);
|
|
|
|
#if _FFR_QUARANTINE
|
|
|
|
(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
|
|
|
|
" %6ld", stats.stat_nq[i]);
|
|
|
|
#endif /* _FFR_QUARANTINE */
|
1998-08-03 05:56:20 +00:00
|
|
|
if (mnames)
|
2002-02-17 21:56:45 +00:00
|
|
|
(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
|
|
|
|
" %s",
|
|
|
|
mtable[i]);
|
|
|
|
(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "\n");
|
2000-08-12 21:55:49 +00:00
|
|
|
frmsgs += stats.stat_nf[i];
|
|
|
|
frbytes += stats.stat_bf[i];
|
|
|
|
tomsgs += stats.stat_nt[i];
|
|
|
|
tobytes += stats.stat_bt[i];
|
|
|
|
rejmsgs += stats.stat_nr[i];
|
|
|
|
dismsgs += stats.stat_nd[i];
|
2002-02-17 21:56:45 +00:00
|
|
|
#if _FFR_QUARANTINE
|
|
|
|
quarmsgs += stats.stat_nq[i];
|
|
|
|
#endif /* _FFR_QUARANTINE */
|
1998-08-03 05:56:20 +00:00
|
|
|
}
|
|
|
|
}
|
1999-01-12 12:26:57 +00:00
|
|
|
if (progmode)
|
|
|
|
{
|
2002-02-17 21:56:45 +00:00
|
|
|
(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
|
|
|
|
" T %8ld %10ld %8ld %10ld %6ld %6ld",
|
|
|
|
frmsgs, frbytes, tomsgs, tobytes, rejmsgs,
|
|
|
|
dismsgs);
|
|
|
|
#if _FFR_QUARANTINE
|
|
|
|
(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
|
|
|
|
" %6ld", quarmsgs);
|
|
|
|
#endif /* _FFR_QUARANTINE */
|
|
|
|
(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "\n");
|
|
|
|
(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
|
|
|
|
" C %8ld %8ld %6ld\n",
|
|
|
|
stats.stat_cf, stats.stat_ct,
|
|
|
|
stats.stat_cr);
|
2000-08-12 21:55:49 +00:00
|
|
|
(void) close(fd);
|
2002-02-17 21:56:45 +00:00
|
|
|
if (trunc)
|
|
|
|
{
|
|
|
|
fd = open(sfile, O_RDWR | O_TRUNC);
|
|
|
|
if (fd >= 0)
|
|
|
|
(void) close(fd);
|
|
|
|
}
|
1999-01-12 12:26:57 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-02-17 21:56:45 +00:00
|
|
|
(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
|
|
|
|
"=============================================================");
|
|
|
|
#if _FFR_QUARANTINE
|
|
|
|
(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "========");
|
|
|
|
#endif /* _FFR_QUARANTINE */
|
|
|
|
(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "\n");
|
|
|
|
(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
|
|
|
|
" T %8ld %10ldK %8ld %10ldK %6ld %6ld",
|
|
|
|
frmsgs, frbytes, tomsgs, tobytes, rejmsgs,
|
|
|
|
dismsgs);
|
|
|
|
#if _FFR_QUARANTINE
|
|
|
|
(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
|
|
|
|
" %6ld", quarmsgs);
|
|
|
|
#endif /* _FFR_QUARANTINE */
|
|
|
|
(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "\n");
|
|
|
|
(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
|
|
|
|
" C %8ld %10s %8ld %10s %6ld\n",
|
|
|
|
stats.stat_cf, "", stats.stat_ct, "",
|
|
|
|
stats.stat_cr);
|
1999-01-12 12:26:57 +00:00
|
|
|
}
|
1998-08-03 05:56:20 +00:00
|
|
|
exit(EX_OK);
|
2000-08-12 21:55:49 +00:00
|
|
|
/* NOTREACHED */
|
|
|
|
return EX_OK;
|
1998-08-03 05:56:20 +00:00
|
|
|
}
|