1*ba1276acSMatthew Dillon /*
2*ba1276acSMatthew Dillon * Copyright 2010 Red Hat, Inc. All rights reserved.
3*ba1276acSMatthew Dillon * Use is subject to license terms.
4*ba1276acSMatthew Dillon *
5*ba1276acSMatthew Dillon * Redistribution and use in source and binary forms, with or without
6*ba1276acSMatthew Dillon * modification, are permitted provided that the following conditions
7*ba1276acSMatthew Dillon * are met:
8*ba1276acSMatthew Dillon * 1. Redistributions of source code must retain the above copyright
9*ba1276acSMatthew Dillon * notice, this list of conditions and the following disclaimer.
10*ba1276acSMatthew Dillon * 2. Redistributions in binary form must reproduce the above copyright
11*ba1276acSMatthew Dillon * notice, this list of conditions and the following disclaimer in the
12*ba1276acSMatthew Dillon * documentation and/or other materials provided with the distribution.
13*ba1276acSMatthew Dillon *
14*ba1276acSMatthew Dillon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15*ba1276acSMatthew Dillon * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16*ba1276acSMatthew Dillon * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17*ba1276acSMatthew Dillon * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18*ba1276acSMatthew Dillon * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19*ba1276acSMatthew Dillon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20*ba1276acSMatthew Dillon * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21*ba1276acSMatthew Dillon * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22*ba1276acSMatthew Dillon * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23*ba1276acSMatthew Dillon * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24*ba1276acSMatthew Dillon *
25*ba1276acSMatthew Dillon * Red Hat author: Jan F. Chadima <jchadima@redhat.com>
26*ba1276acSMatthew Dillon */
27*ba1276acSMatthew Dillon
28*ba1276acSMatthew Dillon #include "includes.h"
29*ba1276acSMatthew Dillon #if defined(USE_LINUX_AUDIT)
30*ba1276acSMatthew Dillon #include <libaudit.h>
31*ba1276acSMatthew Dillon #include <unistd.h>
32*ba1276acSMatthew Dillon #include <string.h>
33*ba1276acSMatthew Dillon
34*ba1276acSMatthew Dillon #include "log.h"
35*ba1276acSMatthew Dillon #include "audit.h"
36*ba1276acSMatthew Dillon #include "canohost.h"
37*ba1276acSMatthew Dillon #include "packet.h"
38*ba1276acSMatthew Dillon
39*ba1276acSMatthew Dillon const char *audit_username(void);
40*ba1276acSMatthew Dillon
41*ba1276acSMatthew Dillon int
linux_audit_record_event(int uid,const char * username,const char * hostname,const char * ip,const char * ttyn,int success)42*ba1276acSMatthew Dillon linux_audit_record_event(int uid, const char *username, const char *hostname,
43*ba1276acSMatthew Dillon const char *ip, const char *ttyn, int success)
44*ba1276acSMatthew Dillon {
45*ba1276acSMatthew Dillon int audit_fd, rc, saved_errno;
46*ba1276acSMatthew Dillon
47*ba1276acSMatthew Dillon if ((audit_fd = audit_open()) < 0) {
48*ba1276acSMatthew Dillon if (errno == EINVAL || errno == EPROTONOSUPPORT ||
49*ba1276acSMatthew Dillon errno == EAFNOSUPPORT)
50*ba1276acSMatthew Dillon return 1; /* No audit support in kernel */
51*ba1276acSMatthew Dillon else
52*ba1276acSMatthew Dillon return 0; /* Must prevent login */
53*ba1276acSMatthew Dillon }
54*ba1276acSMatthew Dillon rc = audit_log_acct_message(audit_fd, AUDIT_USER_LOGIN,
55*ba1276acSMatthew Dillon NULL, "login", username ? username : "(unknown)",
56*ba1276acSMatthew Dillon username == NULL ? uid : -1, hostname, ip, ttyn, success);
57*ba1276acSMatthew Dillon saved_errno = errno;
58*ba1276acSMatthew Dillon close(audit_fd);
59*ba1276acSMatthew Dillon
60*ba1276acSMatthew Dillon /*
61*ba1276acSMatthew Dillon * Do not report error if the error is EPERM and sshd is run as non
62*ba1276acSMatthew Dillon * root user.
63*ba1276acSMatthew Dillon */
64*ba1276acSMatthew Dillon if ((rc == -EPERM) && (geteuid() != 0))
65*ba1276acSMatthew Dillon rc = 0;
66*ba1276acSMatthew Dillon errno = saved_errno;
67*ba1276acSMatthew Dillon
68*ba1276acSMatthew Dillon return rc >= 0;
69*ba1276acSMatthew Dillon }
70*ba1276acSMatthew Dillon
71*ba1276acSMatthew Dillon /* Below is the sshd audit API code */
72*ba1276acSMatthew Dillon
73*ba1276acSMatthew Dillon void
audit_connection_from(const char * host,int port)74*ba1276acSMatthew Dillon audit_connection_from(const char *host, int port)
75*ba1276acSMatthew Dillon {
76*ba1276acSMatthew Dillon /* not implemented */
77*ba1276acSMatthew Dillon }
78*ba1276acSMatthew Dillon
79*ba1276acSMatthew Dillon void
audit_run_command(const char * command)80*ba1276acSMatthew Dillon audit_run_command(const char *command)
81*ba1276acSMatthew Dillon {
82*ba1276acSMatthew Dillon /* not implemented */
83*ba1276acSMatthew Dillon }
84*ba1276acSMatthew Dillon
85*ba1276acSMatthew Dillon void
audit_session_open(struct logininfo * li)86*ba1276acSMatthew Dillon audit_session_open(struct logininfo *li)
87*ba1276acSMatthew Dillon {
88*ba1276acSMatthew Dillon if (linux_audit_record_event(li->uid, NULL, li->hostname, NULL,
89*ba1276acSMatthew Dillon li->line, 1) == 0)
90*ba1276acSMatthew Dillon fatal("linux_audit_write_entry failed: %s", strerror(errno));
91*ba1276acSMatthew Dillon }
92*ba1276acSMatthew Dillon
93*ba1276acSMatthew Dillon void
audit_session_close(struct logininfo * li)94*ba1276acSMatthew Dillon audit_session_close(struct logininfo *li)
95*ba1276acSMatthew Dillon {
96*ba1276acSMatthew Dillon /* not implemented */
97*ba1276acSMatthew Dillon }
98*ba1276acSMatthew Dillon
99*ba1276acSMatthew Dillon void
audit_event(struct ssh * ssh,ssh_audit_event_t event)100*ba1276acSMatthew Dillon audit_event(struct ssh *ssh, ssh_audit_event_t event)
101*ba1276acSMatthew Dillon {
102*ba1276acSMatthew Dillon switch(event) {
103*ba1276acSMatthew Dillon case SSH_AUTH_SUCCESS:
104*ba1276acSMatthew Dillon case SSH_CONNECTION_CLOSE:
105*ba1276acSMatthew Dillon case SSH_NOLOGIN:
106*ba1276acSMatthew Dillon case SSH_LOGIN_EXCEED_MAXTRIES:
107*ba1276acSMatthew Dillon case SSH_LOGIN_ROOT_DENIED:
108*ba1276acSMatthew Dillon break;
109*ba1276acSMatthew Dillon case SSH_AUTH_FAIL_NONE:
110*ba1276acSMatthew Dillon case SSH_AUTH_FAIL_PASSWD:
111*ba1276acSMatthew Dillon case SSH_AUTH_FAIL_KBDINT:
112*ba1276acSMatthew Dillon case SSH_AUTH_FAIL_PUBKEY:
113*ba1276acSMatthew Dillon case SSH_AUTH_FAIL_HOSTBASED:
114*ba1276acSMatthew Dillon case SSH_AUTH_FAIL_GSSAPI:
115*ba1276acSMatthew Dillon case SSH_INVALID_USER:
116*ba1276acSMatthew Dillon linux_audit_record_event(-1, audit_username(), NULL,
117*ba1276acSMatthew Dillon ssh_remote_ipaddr(ssh), "sshd", 0);
118*ba1276acSMatthew Dillon break;
119*ba1276acSMatthew Dillon default:
120*ba1276acSMatthew Dillon debug("%s: unhandled event %d", __func__, event);
121*ba1276acSMatthew Dillon break;
122*ba1276acSMatthew Dillon }
123*ba1276acSMatthew Dillon }
124*ba1276acSMatthew Dillon #endif /* USE_LINUX_AUDIT */
125