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