#include <sys/types.h>
#include <bsm/libbsm.h>
#include <bsm/audit_uevents.h>
#include <err.h>
#include <errno.h>
#include <pwd.h>
#include <stdio.h>
#include <strings.h>
#include <unistd.h>
#include "login.h"
static au_tid_t tid;
void
au_login_success(void)
{
token_t *tok;
int aufd;
au_mask_t aumask;
auditinfo_t auinfo;
uid_t uid = pwd->pw_uid;
gid_t gid = pwd->pw_gid;
pid_t pid = getpid();
int au_cond;
if (auditon(A_GETCOND, &au_cond, sizeof(au_cond)) < 0) {
if (errno == ENOSYS)
return;
errx(1, "could not determine audit condition");
}
if (au_cond == AUC_NOAUDIT)
return;
if (au_user_mask(pwd->pw_name, &aumask) == -1)
errx(1, "could not calculate audit mask");
auinfo.ai_auid = uid;
auinfo.ai_asid = pid;
bcopy(&tid, &auinfo.ai_termid, sizeof(auinfo.ai_termid));
bcopy(&aumask, &auinfo.ai_mask, sizeof(auinfo.ai_mask));
if (setaudit(&auinfo) != 0)
err(1, "setaudit failed");
if ((aufd = au_open()) == -1)
errx(1, "audit error: au_open() failed");
if ((tok = au_to_subject32(uid, geteuid(), getegid(), uid, gid, pid,
pid, &tid)) == NULL)
errx(1, "audit error: au_to_subject32() failed");
au_write(aufd, tok);
if ((tok = au_to_return32(0, 0)) == NULL)
errx(1, "audit error: au_to_return32() failed");
au_write(aufd, tok);
if (au_close(aufd, 1, AUE_login) == -1)
errx(1, "audit record was not committed.");
}
void
au_login_fail(const char *errmsg, int na)
{
token_t *tok;
int aufd;
int au_cond;
uid_t uid;
gid_t gid;
pid_t pid = getpid();
if (auditon(A_GETCOND, &au_cond, sizeof(au_cond)) < 0) {
if (errno == ENOSYS)
return;
errx(1, "could not determine audit condition");
}
if (au_cond == AUC_NOAUDIT)
return;
if ((aufd = au_open()) == -1)
errx(1, "audit error: au_open() failed");
if (na) {
if ((tok = au_to_subject32(-1, geteuid(), getegid(), -1, -1,
pid, -1, &tid)) == NULL)
errx(1, "audit error: au_to_subject32() failed");
} else {
uid = pwd->pw_uid;
gid = pwd->pw_gid;
if ((tok = au_to_subject32(uid, geteuid(), getegid(), uid,
gid, pid, pid, &tid)) == NULL)
errx(1, "audit error: au_to_subject32() failed");
}
au_write(aufd, tok);
if ((tok = au_to_text(errmsg)) == NULL)
errx(1, "audit error: au_to_text() failed");
au_write(aufd, tok);
if ((tok = au_to_return32(1, errno)) == NULL)
errx(1, "audit error: au_to_return32() failed");
au_write(aufd, tok);
if (au_close(aufd, 1, AUE_login) == -1)
errx(1, "audit error: au_close() was not committed");
}
void
audit_logout(void)
{
token_t *tok;
int aufd;
uid_t uid = pwd->pw_uid;
gid_t gid = pwd->pw_gid;
pid_t pid = getpid();
int au_cond;
if (auditon(A_GETCOND, &au_cond, sizeof(au_cond)) < 0) {
if (errno == ENOSYS)
return;
errx(1, "could not determine audit condition");
}
if (au_cond == AUC_NOAUDIT)
return;
if ((aufd = au_open()) == -1)
errx(1, "audit error: au_open() failed");
if ((tok = au_to_subject32(uid, geteuid(), getegid(), uid, gid, pid,
pid, &tid)) == NULL)
errx(1, "audit error: au_to_subject32() failed");
au_write(aufd, tok);
if ((tok = au_to_return32(0, 0)) == NULL)
errx(1, "audit error: au_to_return32() failed");
au_write(aufd, tok);
if (au_close(aufd, 1, AUE_logout) == -1)
errx(1, "audit record was not committed.");
}