xref: /dflybsd-src/crypto/openssh/audit.c (revision ba1276acd1c8c22d225b1bcf370a14c878644f44)
1*ba1276acSMatthew Dillon /*
2*ba1276acSMatthew Dillon  * Copyright (c) 2004, 2005 Darren Tucker.  All rights reserved.
3*ba1276acSMatthew Dillon  *
4*ba1276acSMatthew Dillon  * Redistribution and use in source and binary forms, with or without
5*ba1276acSMatthew Dillon  * modification, are permitted provided that the following conditions
6*ba1276acSMatthew Dillon  * are met:
7*ba1276acSMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
8*ba1276acSMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
9*ba1276acSMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
10*ba1276acSMatthew Dillon  *    notice, this list of conditions and the following disclaimer in the
11*ba1276acSMatthew Dillon  *    documentation and/or other materials provided with the distribution.
12*ba1276acSMatthew Dillon  *
13*ba1276acSMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14*ba1276acSMatthew Dillon  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15*ba1276acSMatthew Dillon  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16*ba1276acSMatthew Dillon  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17*ba1276acSMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18*ba1276acSMatthew Dillon  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19*ba1276acSMatthew Dillon  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20*ba1276acSMatthew Dillon  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21*ba1276acSMatthew Dillon  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22*ba1276acSMatthew Dillon  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23*ba1276acSMatthew Dillon  */
24*ba1276acSMatthew Dillon 
25*ba1276acSMatthew Dillon #include "includes.h"
26*ba1276acSMatthew Dillon 
27*ba1276acSMatthew Dillon #include <stdarg.h>
28*ba1276acSMatthew Dillon #include <string.h>
29*ba1276acSMatthew Dillon #include <unistd.h>
30*ba1276acSMatthew Dillon 
31*ba1276acSMatthew Dillon #ifdef SSH_AUDIT_EVENTS
32*ba1276acSMatthew Dillon 
33*ba1276acSMatthew Dillon #include "audit.h"
34*ba1276acSMatthew Dillon #include "log.h"
35*ba1276acSMatthew Dillon #include "hostfile.h"
36*ba1276acSMatthew Dillon #include "auth.h"
37*ba1276acSMatthew Dillon 
38*ba1276acSMatthew Dillon /*
39*ba1276acSMatthew Dillon  * Care must be taken when using this since it WILL NOT be initialized when
40*ba1276acSMatthew Dillon  * audit_connection_from() is called and MAY NOT be initialized when
41*ba1276acSMatthew Dillon  * audit_event(CONNECTION_ABANDON) is called.  Test for NULL before using.
42*ba1276acSMatthew Dillon  */
43*ba1276acSMatthew Dillon extern Authctxt *the_authctxt;
44*ba1276acSMatthew Dillon 
45*ba1276acSMatthew Dillon /* Maybe add the audit class to struct Authmethod? */
46*ba1276acSMatthew Dillon ssh_audit_event_t
audit_classify_auth(const char * method)47*ba1276acSMatthew Dillon audit_classify_auth(const char *method)
48*ba1276acSMatthew Dillon {
49*ba1276acSMatthew Dillon 	if (strcmp(method, "none") == 0)
50*ba1276acSMatthew Dillon 		return SSH_AUTH_FAIL_NONE;
51*ba1276acSMatthew Dillon 	else if (strcmp(method, "password") == 0)
52*ba1276acSMatthew Dillon 		return SSH_AUTH_FAIL_PASSWD;
53*ba1276acSMatthew Dillon 	else if (strcmp(method, "publickey") == 0 ||
54*ba1276acSMatthew Dillon 	    strcmp(method, "rsa") == 0)
55*ba1276acSMatthew Dillon 		return SSH_AUTH_FAIL_PUBKEY;
56*ba1276acSMatthew Dillon 	else if (strncmp(method, "keyboard-interactive", 20) == 0 ||
57*ba1276acSMatthew Dillon 	    strcmp(method, "challenge-response") == 0)
58*ba1276acSMatthew Dillon 		return SSH_AUTH_FAIL_KBDINT;
59*ba1276acSMatthew Dillon 	else if (strcmp(method, "hostbased") == 0 ||
60*ba1276acSMatthew Dillon 	    strcmp(method, "rhosts-rsa") == 0)
61*ba1276acSMatthew Dillon 		return SSH_AUTH_FAIL_HOSTBASED;
62*ba1276acSMatthew Dillon 	else if (strcmp(method, "gssapi-with-mic") == 0)
63*ba1276acSMatthew Dillon 		return SSH_AUTH_FAIL_GSSAPI;
64*ba1276acSMatthew Dillon 	else
65*ba1276acSMatthew Dillon 		return SSH_AUDIT_UNKNOWN;
66*ba1276acSMatthew Dillon }
67*ba1276acSMatthew Dillon 
68*ba1276acSMatthew Dillon /* helper to return supplied username */
69*ba1276acSMatthew Dillon const char *
audit_username(void)70*ba1276acSMatthew Dillon audit_username(void)
71*ba1276acSMatthew Dillon {
72*ba1276acSMatthew Dillon 	static const char unknownuser[] = "(unknown user)";
73*ba1276acSMatthew Dillon 	static const char invaliduser[] = "(invalid user)";
74*ba1276acSMatthew Dillon 
75*ba1276acSMatthew Dillon 	if (the_authctxt == NULL || the_authctxt->user == NULL)
76*ba1276acSMatthew Dillon 		return (unknownuser);
77*ba1276acSMatthew Dillon 	if (!the_authctxt->valid)
78*ba1276acSMatthew Dillon 		return (invaliduser);
79*ba1276acSMatthew Dillon 	return (the_authctxt->user);
80*ba1276acSMatthew Dillon }
81*ba1276acSMatthew Dillon 
82*ba1276acSMatthew Dillon const char *
audit_event_lookup(ssh_audit_event_t ev)83*ba1276acSMatthew Dillon audit_event_lookup(ssh_audit_event_t ev)
84*ba1276acSMatthew Dillon {
85*ba1276acSMatthew Dillon 	int i;
86*ba1276acSMatthew Dillon 	static struct event_lookup_struct {
87*ba1276acSMatthew Dillon 		ssh_audit_event_t event;
88*ba1276acSMatthew Dillon 		const char *name;
89*ba1276acSMatthew Dillon 	} event_lookup[] = {
90*ba1276acSMatthew Dillon 		{SSH_LOGIN_EXCEED_MAXTRIES,	"LOGIN_EXCEED_MAXTRIES"},
91*ba1276acSMatthew Dillon 		{SSH_LOGIN_ROOT_DENIED,		"LOGIN_ROOT_DENIED"},
92*ba1276acSMatthew Dillon 		{SSH_AUTH_SUCCESS,		"AUTH_SUCCESS"},
93*ba1276acSMatthew Dillon 		{SSH_AUTH_FAIL_NONE,		"AUTH_FAIL_NONE"},
94*ba1276acSMatthew Dillon 		{SSH_AUTH_FAIL_PASSWD,		"AUTH_FAIL_PASSWD"},
95*ba1276acSMatthew Dillon 		{SSH_AUTH_FAIL_KBDINT,		"AUTH_FAIL_KBDINT"},
96*ba1276acSMatthew Dillon 		{SSH_AUTH_FAIL_PUBKEY,		"AUTH_FAIL_PUBKEY"},
97*ba1276acSMatthew Dillon 		{SSH_AUTH_FAIL_HOSTBASED,	"AUTH_FAIL_HOSTBASED"},
98*ba1276acSMatthew Dillon 		{SSH_AUTH_FAIL_GSSAPI,		"AUTH_FAIL_GSSAPI"},
99*ba1276acSMatthew Dillon 		{SSH_INVALID_USER,		"INVALID_USER"},
100*ba1276acSMatthew Dillon 		{SSH_NOLOGIN,			"NOLOGIN"},
101*ba1276acSMatthew Dillon 		{SSH_CONNECTION_CLOSE,		"CONNECTION_CLOSE"},
102*ba1276acSMatthew Dillon 		{SSH_CONNECTION_ABANDON,	"CONNECTION_ABANDON"},
103*ba1276acSMatthew Dillon 		{SSH_AUDIT_UNKNOWN,		"AUDIT_UNKNOWN"}
104*ba1276acSMatthew Dillon 	};
105*ba1276acSMatthew Dillon 
106*ba1276acSMatthew Dillon 	for (i = 0; event_lookup[i].event != SSH_AUDIT_UNKNOWN; i++)
107*ba1276acSMatthew Dillon 		if (event_lookup[i].event == ev)
108*ba1276acSMatthew Dillon 			break;
109*ba1276acSMatthew Dillon 	return(event_lookup[i].name);
110*ba1276acSMatthew Dillon }
111*ba1276acSMatthew Dillon 
112*ba1276acSMatthew Dillon # ifndef CUSTOM_SSH_AUDIT_EVENTS
113*ba1276acSMatthew Dillon /*
114*ba1276acSMatthew Dillon  * Null implementations of audit functions.
115*ba1276acSMatthew Dillon  * These get used if SSH_AUDIT_EVENTS is defined but no audit module is enabled.
116*ba1276acSMatthew Dillon  */
117*ba1276acSMatthew Dillon 
118*ba1276acSMatthew Dillon /*
119*ba1276acSMatthew Dillon  * Called after a connection has been accepted but before any authentication
120*ba1276acSMatthew Dillon  * has been attempted.
121*ba1276acSMatthew Dillon  */
122*ba1276acSMatthew Dillon void
audit_connection_from(const char * host,int port)123*ba1276acSMatthew Dillon audit_connection_from(const char *host, int port)
124*ba1276acSMatthew Dillon {
125*ba1276acSMatthew Dillon 	debug("audit connection from %s port %d euid %d", host, port,
126*ba1276acSMatthew Dillon 	    (int)geteuid());
127*ba1276acSMatthew Dillon }
128*ba1276acSMatthew Dillon 
129*ba1276acSMatthew Dillon /*
130*ba1276acSMatthew Dillon  * Called when various events occur (see audit.h for a list of possible
131*ba1276acSMatthew Dillon  * events and what they mean).
132*ba1276acSMatthew Dillon  */
133*ba1276acSMatthew Dillon void
audit_event(struct ssh * ssh,ssh_audit_event_t event)134*ba1276acSMatthew Dillon audit_event(struct ssh *ssh, ssh_audit_event_t event)
135*ba1276acSMatthew Dillon {
136*ba1276acSMatthew Dillon 	debug("audit event euid %d user %s event %d (%s)", geteuid(),
137*ba1276acSMatthew Dillon 	    audit_username(), event, audit_event_lookup(event));
138*ba1276acSMatthew Dillon }
139*ba1276acSMatthew Dillon 
140*ba1276acSMatthew Dillon /*
141*ba1276acSMatthew Dillon  * Called when a user session is started.  Argument is the tty allocated to
142*ba1276acSMatthew Dillon  * the session, or NULL if no tty was allocated.
143*ba1276acSMatthew Dillon  *
144*ba1276acSMatthew Dillon  * Note that this may be called multiple times if multiple sessions are used
145*ba1276acSMatthew Dillon  * within a single connection.
146*ba1276acSMatthew Dillon  */
147*ba1276acSMatthew Dillon void
audit_session_open(struct logininfo * li)148*ba1276acSMatthew Dillon audit_session_open(struct logininfo *li)
149*ba1276acSMatthew Dillon {
150*ba1276acSMatthew Dillon 	const char *t = li->line ? li->line : "(no tty)";
151*ba1276acSMatthew Dillon 
152*ba1276acSMatthew Dillon 	debug("audit session open euid %d user %s tty name %s", geteuid(),
153*ba1276acSMatthew Dillon 	    audit_username(), t);
154*ba1276acSMatthew Dillon }
155*ba1276acSMatthew Dillon 
156*ba1276acSMatthew Dillon /*
157*ba1276acSMatthew Dillon  * Called when a user session is closed.  Argument is the tty allocated to
158*ba1276acSMatthew Dillon  * the session, or NULL if no tty was allocated.
159*ba1276acSMatthew Dillon  *
160*ba1276acSMatthew Dillon  * Note that this may be called multiple times if multiple sessions are used
161*ba1276acSMatthew Dillon  * within a single connection.
162*ba1276acSMatthew Dillon  */
163*ba1276acSMatthew Dillon void
audit_session_close(struct logininfo * li)164*ba1276acSMatthew Dillon audit_session_close(struct logininfo *li)
165*ba1276acSMatthew Dillon {
166*ba1276acSMatthew Dillon 	const char *t = li->line ? li->line : "(no tty)";
167*ba1276acSMatthew Dillon 
168*ba1276acSMatthew Dillon 	debug("audit session close euid %d user %s tty name %s", geteuid(),
169*ba1276acSMatthew Dillon 	    audit_username(), t);
170*ba1276acSMatthew Dillon }
171*ba1276acSMatthew Dillon 
172*ba1276acSMatthew Dillon /*
173*ba1276acSMatthew Dillon  * This will be called when a user runs a non-interactive command.  Note that
174*ba1276acSMatthew Dillon  * it may be called multiple times for a single connection since SSH2 allows
175*ba1276acSMatthew Dillon  * multiple sessions within a single connection.
176*ba1276acSMatthew Dillon  */
177*ba1276acSMatthew Dillon void
audit_run_command(const char * command)178*ba1276acSMatthew Dillon audit_run_command(const char *command)
179*ba1276acSMatthew Dillon {
180*ba1276acSMatthew Dillon 	debug("audit run command euid %d user %s command '%.200s'", geteuid(),
181*ba1276acSMatthew Dillon 	    audit_username(), command);
182*ba1276acSMatthew Dillon }
183*ba1276acSMatthew Dillon # endif  /* !defined CUSTOM_SSH_AUDIT_EVENTS */
184*ba1276acSMatthew Dillon #endif /* SSH_AUDIT_EVENTS */
185