#include <dirent.h>
#include <errno.h>
#include <locale.h>
#include <libintl.h>
#include <stdlib.h>
#include <strings.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/file.h>
#include <bsm/audit.h>
#include <bsm/audit_record.h>
#include <bsm/libbsm.h>
#include "praudit.h"
#include "toktable.h"
static int process_options(int *argc, char *argv[], char *names[]);
static int input_mode;
static int format = PRF_DEFAULTM;
static char SEPARATOR[SEP_SIZE] = ",";
static FILE *gf = NULL;
static FILE *pf = NULL;
int
main(int argc, char **argv)
{
int i = 0, retstat;
char *names[MAXFILENAMES];
(void) setlocale(LC_ALL, "");
(void) textdomain(TEXT_DOMAIN);
if ((retstat = process_options(&argc, argv, names)) == 0) {
if (pf != NULL) {
errno = 0;
loadnames(pf);
(void) fclose(pf);
if (errno != 0) {
(void) fprintf(stderr,
gettext("praudit: Problem reading passwd "
"file.\n"));
exit(1);
}
}
if (gf != NULL) {
errno = 0;
loadgroups(gf);
(void) fclose(gf);
if (errno != 0) {
(void) fprintf(stderr,
gettext("praudit: Problem reading group "
"file.\n"));
exit(1);
}
}
if (format & PRF_XMLM)
print_audit_xml_prolog();
do {
retstat = 0;
if (input_mode == FILEMODE) {
if (freopen(names[i], "r", stdin) == NULL) {
(void) fprintf(stderr,
gettext("praudit: Cannot associate "
"stdin with %s: %s\n"),
names[i], strerror(errno));
exit(1);
}
}
retstat = print_audit(format, SEPARATOR);
} while ((++i < argc) && retstat >= 0);
}
if ((retstat == 0) && (format & PRF_XMLM))
print_audit_xml_ending();
if (retstat == -2) {
(void) printf(gettext("\nusage: praudit [-r/-s] [-l] [-x] "
"[-ddel] [-p file] [-g file] [-c] filename...\n"));
exit(1);
} else if (retstat < 0) {
exit(1);
}
return (0);
}
int
process_options(int *argc, char **argv, char **names)
{
int c, returnstat = 0;
while ((c = getopt(*argc, argv, "crslxd:g:p:")) != -1) {
switch (c) {
case 'c':
format |= PRF_NOCACHE;
break;
case 'r':
if (format & PRF_SHORTM)
returnstat = -2;
else
format |= PRF_RAWM;
break;
case 's':
if (format & PRF_RAWM)
returnstat = -2;
else
format |= PRF_SHORTM;
break;
case 'l':
format |= PRF_ONELINE;
break;
case 'x':
format |= PRF_XMLM;
break;
case 'd':
if (strlen(optarg) < sizeof (SEPARATOR))
(void) strlcpy(SEPARATOR, optarg,
sizeof (SEPARATOR));
else {
(void) fprintf(stderr,
gettext("praudit: Delimiter too "
"long. Using default.\n"));
}
break;
case 'g':
if ((gf = fopen(optarg, "r")) == NULL) {
(void) fprintf(stderr, gettext("praudit: Cannot"
" open specified group file.\n"));
return (-1);
}
break;
case 'p':
if ((pf = fopen(optarg, "r")) == NULL) {
(void) fprintf(stderr, gettext("praudit: Cannot"
" open specified passwd file.\n"));
return (-1);
}
break;
default:
returnstat = -2;
break;
}
}
argv = &argv[optind - 1];
*argc -= optind;
if (*argc > MAXFILENAMES) {
(void) fprintf(stderr, gettext("praudit: Too many file "
"names.\n"));
return (-1);
}
if (*argc > 0) {
int count = *argc;
input_mode = FILEMODE;
do {
*names++ = *++argv;
} while (--count > 0);
} else
input_mode = PIPEMODE;
return (returnstat);
}