1146998Sdes/*
2146998Sdes * Copyright (c) 2004, 2005 Darren Tucker.  All rights reserved.
3146998Sdes *
4146998Sdes * Redistribution and use in source and binary forms, with or without
5146998Sdes * modification, are permitted provided that the following conditions
6146998Sdes * are met:
7146998Sdes * 1. Redistributions of source code must retain the above copyright
8146998Sdes *    notice, this list of conditions and the following disclaimer.
9146998Sdes * 2. Redistributions in binary form must reproduce the above copyright
10146998Sdes *    notice, this list of conditions and the following disclaimer in the
11146998Sdes *    documentation and/or other materials provided with the distribution.
12146998Sdes *
13146998Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14146998Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15146998Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16146998Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17146998Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18146998Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19146998Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20146998Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21146998Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22146998Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23146998Sdes */
24146998Sdes
25146998Sdes#include "includes.h"
26146998Sdes
27162852Sdes#include <stdarg.h>
28162852Sdes#include <string.h>
29162852Sdes
30146998Sdes#ifdef SSH_AUDIT_EVENTS
31146998Sdes
32146998Sdes#include "audit.h"
33146998Sdes#include "log.h"
34162852Sdes#include "key.h"
35162852Sdes#include "hostfile.h"
36146998Sdes#include "auth.h"
37146998Sdes
38146998Sdes/*
39146998Sdes * Care must be taken when using this since it WILL NOT be initialized when
40146998Sdes * audit_connection_from() is called and MAY NOT be initialized when
41146998Sdes * audit_event(CONNECTION_ABANDON) is called.  Test for NULL before using.
42146998Sdes */
43146998Sdesextern Authctxt *the_authctxt;
44146998Sdes
45146998Sdes/* Maybe add the audit class to struct Authmethod? */
46146998Sdesssh_audit_event_t
47146998Sdesaudit_classify_auth(const char *method)
48146998Sdes{
49146998Sdes	if (strcmp(method, "none") == 0)
50146998Sdes		return SSH_AUTH_FAIL_NONE;
51146998Sdes	else if (strcmp(method, "password") == 0)
52146998Sdes		return SSH_AUTH_FAIL_PASSWD;
53146998Sdes	else if (strcmp(method, "publickey") == 0 ||
54146998Sdes	    strcmp(method, "rsa") == 0)
55146998Sdes		return SSH_AUTH_FAIL_PUBKEY;
56146998Sdes	else if (strncmp(method, "keyboard-interactive", 20) == 0 ||
57146998Sdes	    strcmp(method, "challenge-response") == 0)
58146998Sdes		return SSH_AUTH_FAIL_KBDINT;
59146998Sdes	else if (strcmp(method, "hostbased") == 0 ||
60146998Sdes	    strcmp(method, "rhosts-rsa") == 0)
61146998Sdes		return SSH_AUTH_FAIL_HOSTBASED;
62146998Sdes	else if (strcmp(method, "gssapi-with-mic") == 0)
63146998Sdes		return SSH_AUTH_FAIL_GSSAPI;
64146998Sdes	else
65146998Sdes		return SSH_AUDIT_UNKNOWN;
66146998Sdes}
67146998Sdes
68146998Sdes/* helper to return supplied username */
69146998Sdesconst char *
70146998Sdesaudit_username(void)
71146998Sdes{
72146998Sdes	static const char unknownuser[] = "(unknown user)";
73146998Sdes	static const char invaliduser[] = "(invalid user)";
74146998Sdes
75146998Sdes	if (the_authctxt == NULL || the_authctxt->user == NULL)
76146998Sdes		return (unknownuser);
77146998Sdes	if (!the_authctxt->valid)
78146998Sdes		return (invaliduser);
79146998Sdes	return (the_authctxt->user);
80146998Sdes}
81146998Sdes
82146998Sdesconst char *
83146998Sdesaudit_event_lookup(ssh_audit_event_t ev)
84146998Sdes{
85146998Sdes	int i;
86146998Sdes	static struct event_lookup_struct {
87146998Sdes		ssh_audit_event_t event;
88146998Sdes		const char *name;
89146998Sdes	} event_lookup[] = {
90146998Sdes		{SSH_LOGIN_EXCEED_MAXTRIES,	"LOGIN_EXCEED_MAXTRIES"},
91146998Sdes		{SSH_LOGIN_ROOT_DENIED,		"LOGIN_ROOT_DENIED"},
92146998Sdes		{SSH_AUTH_SUCCESS,		"AUTH_SUCCESS"},
93146998Sdes		{SSH_AUTH_FAIL_NONE,		"AUTH_FAIL_NONE"},
94146998Sdes		{SSH_AUTH_FAIL_PASSWD,		"AUTH_FAIL_PASSWD"},
95146998Sdes		{SSH_AUTH_FAIL_KBDINT,		"AUTH_FAIL_KBDINT"},
96146998Sdes		{SSH_AUTH_FAIL_PUBKEY,		"AUTH_FAIL_PUBKEY"},
97146998Sdes		{SSH_AUTH_FAIL_HOSTBASED,	"AUTH_FAIL_HOSTBASED"},
98146998Sdes		{SSH_AUTH_FAIL_GSSAPI,		"AUTH_FAIL_GSSAPI"},
99146998Sdes		{SSH_INVALID_USER,		"INVALID_USER"},
100146998Sdes		{SSH_NOLOGIN,			"NOLOGIN"},
101146998Sdes		{SSH_CONNECTION_CLOSE,		"CONNECTION_CLOSE"},
102146998Sdes		{SSH_CONNECTION_ABANDON,	"CONNECTION_ABANDON"},
103146998Sdes		{SSH_AUDIT_UNKNOWN,		"AUDIT_UNKNOWN"}
104146998Sdes	};
105146998Sdes
106146998Sdes	for (i = 0; event_lookup[i].event != SSH_AUDIT_UNKNOWN; i++)
107146998Sdes		if (event_lookup[i].event == ev)
108146998Sdes			break;
109146998Sdes	return(event_lookup[i].name);
110146998Sdes}
111146998Sdes
112146998Sdes# ifndef CUSTOM_SSH_AUDIT_EVENTS
113146998Sdes/*
114146998Sdes * Null implementations of audit functions.
115146998Sdes * These get used if SSH_AUDIT_EVENTS is defined but no audit module is enabled.
116146998Sdes */
117146998Sdes
118146998Sdes/*
119146998Sdes * Called after a connection has been accepted but before any authentication
120146998Sdes * has been attempted.
121146998Sdes */
122146998Sdesvoid
123146998Sdesaudit_connection_from(const char *host, int port)
124146998Sdes{
125146998Sdes	debug("audit connection from %s port %d euid %d", host, port,
126149749Sdes	    (int)geteuid());
127146998Sdes}
128146998Sdes
129146998Sdes/*
130146998Sdes * Called when various events occur (see audit.h for a list of possible
131146998Sdes * events and what they mean).
132146998Sdes */
133146998Sdesvoid
134146998Sdesaudit_event(ssh_audit_event_t event)
135146998Sdes{
136146998Sdes	debug("audit event euid %d user %s event %d (%s)", geteuid(),
137146998Sdes	    audit_username(), event, audit_event_lookup(event));
138146998Sdes}
139146998Sdes
140146998Sdes/*
141146998Sdes * Called when a user session is started.  Argument is the tty allocated to
142146998Sdes * the session, or NULL if no tty was allocated.
143146998Sdes *
144146998Sdes * Note that this may be called multiple times if multiple sessions are used
145146998Sdes * within a single connection.
146146998Sdes */
147146998Sdesvoid
148221420Sdesaudit_session_open(struct logininfo *li)
149146998Sdes{
150221420Sdes	const char *t = li->line ? li->line : "(no tty)";
151146998Sdes
152146998Sdes	debug("audit session open euid %d user %s tty name %s", geteuid(),
153149749Sdes	    audit_username(), t);
154146998Sdes}
155146998Sdes
156146998Sdes/*
157146998Sdes * Called when a user session is closed.  Argument is the tty allocated to
158146998Sdes * the session, or NULL if no tty was allocated.
159146998Sdes *
160146998Sdes * Note that this may be called multiple times if multiple sessions are used
161146998Sdes * within a single connection.
162146998Sdes */
163146998Sdesvoid
164221420Sdesaudit_session_close(struct logininfo *li)
165146998Sdes{
166221420Sdes	const char *t = li->line ? li->line : "(no tty)";
167146998Sdes
168146998Sdes	debug("audit session close euid %d user %s tty name %s", geteuid(),
169149749Sdes	    audit_username(), t);
170146998Sdes}
171146998Sdes
172146998Sdes/*
173146998Sdes * This will be called when a user runs a non-interactive command.  Note that
174146998Sdes * it may be called multiple times for a single connection since SSH2 allows
175146998Sdes * multiple sessions within a single connection.
176146998Sdes */
177146998Sdesvoid
178146998Sdesaudit_run_command(const char *command)
179146998Sdes{
180146998Sdes	debug("audit run command euid %d user %s command '%.200s'", geteuid(),
181146998Sdes	    audit_username(), command);
182146998Sdes}
183146998Sdes# endif  /* !defined CUSTOM_SSH_AUDIT_EVENTS */
184146998Sdes#endif /* SSH_AUDIT_EVENTS */
185