/*1* Copyright 2010 Red Hat, Inc. All rights reserved.2* Use is subject to license terms.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR14* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES15* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.16* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,17* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT18* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,19* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY20* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT21* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF22* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.23*24* Red Hat author: Jan F. Chadima <[email protected]>25*/2627#include "includes.h"28#if defined(USE_LINUX_AUDIT)29#include <libaudit.h>30#include <unistd.h>31#include <string.h>3233#include "log.h"34#include "audit.h"35#include "canohost.h"36#include "packet.h"3738const char *audit_username(void);3940int41linux_audit_record_event(int uid, const char *username, const char *hostname,42const char *ip, const char *ttyn, int success)43{44int audit_fd, rc, saved_errno;4546if ((audit_fd = audit_open()) < 0) {47if (errno == EINVAL || errno == EPROTONOSUPPORT ||48errno == EAFNOSUPPORT)49return 1; /* No audit support in kernel */50else51return 0; /* Must prevent login */52}53rc = audit_log_acct_message(audit_fd, AUDIT_USER_LOGIN,54NULL, "login", username ? username : "(unknown)",55username == NULL ? uid : -1, hostname, ip, ttyn, success);56saved_errno = errno;57close(audit_fd);5859/*60* Do not report error if the error is EPERM and sshd is run as non61* root user.62*/63if ((rc == -EPERM) && (geteuid() != 0))64rc = 0;65errno = saved_errno;6667return rc >= 0;68}6970/* Below is the sshd audit API code */7172void73audit_connection_from(const char *host, int port)74{75/* not implemented */76}7778void79audit_run_command(const char *command)80{81/* not implemented */82}8384void85audit_session_open(struct logininfo *li)86{87if (linux_audit_record_event(li->uid, NULL, li->hostname, NULL,88li->line, 1) == 0)89fatal("linux_audit_write_entry failed: %s", strerror(errno));90}9192void93audit_session_close(struct logininfo *li)94{95/* not implemented */96}9798void99audit_event(struct ssh *ssh, ssh_audit_event_t event)100{101switch(event) {102case SSH_AUTH_SUCCESS:103case SSH_CONNECTION_CLOSE:104case SSH_NOLOGIN:105case SSH_LOGIN_EXCEED_MAXTRIES:106case SSH_LOGIN_ROOT_DENIED:107break;108case SSH_AUTH_FAIL_NONE:109case SSH_AUTH_FAIL_PASSWD:110case SSH_AUTH_FAIL_KBDINT:111case SSH_AUTH_FAIL_PUBKEY:112case SSH_AUTH_FAIL_HOSTBASED:113case SSH_AUTH_FAIL_GSSAPI:114case SSH_INVALID_USER:115linux_audit_record_event(-1, audit_username(), NULL,116ssh_remote_ipaddr(ssh), "sshd", 0);117break;118default:119debug("%s: unhandled event %d", __func__, event);120break;121}122}123#endif /* USE_LINUX_AUDIT */124125126