1/* $Id: audit-bsm.c,v 1.4 2006/09/01 05:38:36 djm Exp $ */
2
3/*
4 * TODO
5 *
6 * - deal with overlap between this and sys_auth_allowed_user
7 * sys_auth_record_login and record_failed_login.
8 */
9
10/*
11 * Copyright 1988-2002 Sun Microsystems, Inc. All rights reserved.
12 * Use is subject to license terms.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
35/* #pragma ident "@(#)bsmaudit.c 1.1 01/09/17 SMI" */
36
37#include "includes.h"
38#if defined(USE_BSM_AUDIT)
39
40#include <sys/types.h>
41
42#include <stdarg.h>
43#include <unistd.h>
44
45#include "ssh.h"
46#include "log.h"
47#include "key.h"
48#include "hostfile.h"
49#include "auth.h"
50#include "xmalloc.h"
51
52#ifndef AUE_openssh
53# define AUE_openssh 32800
54#endif
55#include <bsm/audit.h>
56#include <bsm/libbsm.h>
57#include <bsm/audit_uevents.h>
58#include <bsm/audit_record.h>
59#include <locale.h>
60
61#if defined(HAVE_GETAUDIT_ADDR)
62#define AuditInfoStruct auditinfo_addr
63#define AuditInfoTermID au_tid_addr_t
64#define GetAuditFunc(a,b) getaudit_addr((a),(b))
65#define GetAuditFuncText "getaudit_addr"
66#define SetAuditFunc(a,b) setaudit_addr((a),(b))
67#define SetAuditFuncText "setaudit_addr"
68#define AUToSubjectFunc au_to_subject_ex
69#define AUToReturnFunc(a,b) au_to_return32((a), (int32_t)(b))
70#else
71#define AuditInfoStruct auditinfo
72#define AuditInfoTermID au_tid_t
73#define GetAuditFunc(a,b) getaudit(a)
74#define GetAuditFuncText "getaudit"
75#define SetAuditFunc(a,b) setaudit(a)
76#define SetAuditFuncText "setaudit"
77#define AUToSubjectFunc au_to_subject
78#define AUToReturnFunc(a,b) au_to_return((a), (u_int)(b))
79#endif
80
81extern int cannot_audit(int);
82extern void aug_init(void);
83extern dev_t aug_get_port(void);
84extern int aug_get_machine(char *, u_int32_t *, u_int32_t *);
85extern void aug_save_auid(au_id_t);
86extern void aug_save_uid(uid_t);
87extern void aug_save_euid(uid_t);
88extern void aug_save_gid(gid_t);
89extern void aug_save_egid(gid_t);
90extern void aug_save_pid(pid_t);
91extern void aug_save_asid(au_asid_t);
92extern void aug_save_tid(dev_t, unsigned int);
93extern void aug_save_tid_ex(dev_t, u_int32_t *, u_int32_t);
94extern int aug_save_me(void);
95extern int aug_save_namask(void);
96extern void aug_save_event(au_event_t);
97extern void aug_save_sorf(int);
98extern void aug_save_text(char *);
99extern void aug_save_text1(char *);
100extern void aug_save_text2(char *);
101extern void aug_save_na(int);
102extern void aug_save_user(char *);
103extern void aug_save_path(char *);
104extern int aug_save_policy(void);
105extern void aug_save_afunc(int (*)(int));
106extern int aug_audit(void);
107extern int aug_na_selected(void);
108extern int aug_selected(void);
109extern int aug_daemon_session(void);
110
111#ifndef HAVE_GETTEXT
112# define gettext(a) (a)
113#endif
114
115extern Authctxt *the_authctxt;
116static AuditInfoTermID ssh_bsm_tid;
117
118/* Below is the low-level BSM interface code */
119
120/*
121 * Check if the specified event is selected (enabled) for auditing.
122 * Returns 1 if the event is selected, 0 if not and -1 on failure.
123 */
124static int
125selected(char *username, uid_t uid, au_event_t event, int sf)
126{
127 int rc, sorf;
128 char naflags[512];
129 struct au_mask mask;
130
131 mask.am_success = mask.am_failure = 0;
132 if (uid < 0) {
133 /* get flags for non-attributable (to a real user) events */
134 rc = getacna(naflags, sizeof(naflags));
135 if (rc == 0)
136 (void) getauditflagsbin(naflags, &mask);
137 } else
138 rc = au_user_mask(username, &mask);
139
140 sorf = (sf == 0) ? AU_PRS_SUCCESS : AU_PRS_FAILURE;
141 return(au_preselect(event, &mask, sorf, AU_PRS_REREAD));
142}
143
144static void
145bsm_audit_record(int typ, char *string, au_event_t event_no)
146{
147 int ad, rc, sel;
148 uid_t uid = -1;
149 gid_t gid = -1;
150 pid_t pid = getpid();
151 AuditInfoTermID tid = ssh_bsm_tid;
152
153 if (the_authctxt != NULL && the_authctxt->valid) {
154 uid = the_authctxt->pw->pw_uid;
155 gid = the_authctxt->pw->pw_gid;
156 }
157
158 rc = (typ == 0) ? 0 : -1;
159 sel = selected(the_authctxt->user, uid, event_no, rc);
160 debug3("BSM audit: typ %d rc %d \"%s\"", typ, rc, string);
161 if (!sel)
162 return; /* audit event does not match mask, do not write */
163
164 debug3("BSM audit: writing audit new record");
165 ad = au_open();
166
167 (void) au_write(ad, AUToSubjectFunc(uid, uid, gid, uid, gid,
168 pid, pid, &tid));
169 (void) au_write(ad, au_to_text(string));
170 (void) au_write(ad, AUToReturnFunc(typ, rc));
171
172 rc = au_close(ad, AU_TO_WRITE, event_no);
173 if (rc < 0)
174 error("BSM audit: %s failed to write \"%s\" record: %s",
175 __func__, string, strerror(errno));
176}
177
178static void
179bsm_audit_session_setup(void)
180{
181 int rc;
182 struct AuditInfoStruct info;
183 au_mask_t mask;
184
185 if (the_authctxt == NULL) {
186 error("BSM audit: session setup internal error (NULL ctxt)");
187 return;
188 }
189
190 if (the_authctxt->valid)
191 info.ai_auid = the_authctxt->pw->pw_uid;
192 else
193 info.ai_auid = -1;
194 info.ai_asid = getpid();
195 mask.am_success = 0;
196 mask.am_failure = 0;
197
198 (void) au_user_mask(the_authctxt->user, &mask);
199
200 info.ai_mask.am_success = mask.am_success;
201 info.ai_mask.am_failure = mask.am_failure;
202
203 info.ai_termid = ssh_bsm_tid;
204
205 rc = SetAuditFunc(&info, sizeof(info));
206 if (rc < 0)
207 error("BSM audit: %s: %s failed: %s", __func__,
208 SetAuditFuncText, strerror(errno));
209}
210
211static void
212bsm_audit_bad_login(const char *what)
213{
214 char textbuf[BSM_TEXTBUFSZ];
215
216 if (the_authctxt->valid) {
217 (void) snprintf(textbuf, sizeof (textbuf),
218 gettext("invalid %s for user %s"),
219 what, the_authctxt->user);
220 bsm_audit_record(4, textbuf, AUE_openssh);
221 } else {
222 (void) snprintf(textbuf, sizeof (textbuf),
223 gettext("invalid user name \"%s\""),
224 the_authctxt->user);
225 bsm_audit_record(3, textbuf, AUE_openssh);
226 }
227}
228
229/* Below is the sshd audit API code */
230
231void
232audit_connection_from(const char *host, int port)
233{
234 AuditInfoTermID *tid = &ssh_bsm_tid;
235 char buf[1024];
236
237 if (cannot_audit(0))
238 return;
239 debug3("BSM audit: connection from %.100s port %d", host, port);
240
241 /* populate our terminal id structure */
242#if defined(HAVE_GETAUDIT_ADDR)
243 tid->at_port = (dev_t)port;
244 aug_get_machine((char *)host, &(tid->at_addr[0]), &(tid->at_type));
245 snprintf(buf, sizeof(buf), "%08x %08x %08x %08x", tid->at_addr[0],
246 tid->at_addr[1], tid->at_addr[2], tid->at_addr[3]);
247 debug3("BSM audit: iptype %d machine ID %s", (int)tid->at_type, buf);
248#else
249 /* this is used on IPv4-only machines */
250 tid->port = (dev_t)port;
251 tid->machine = inet_addr(host);
252 snprintf(buf, sizeof(buf), "%08x", tid->machine);
253 debug3("BSM audit: machine ID %s", buf);
254#endif
255}
256
257void
258audit_run_command(const char *command)
259{
260 /* not implemented */
261}
262
263void
264audit_session_open(const char *ttyn)
265{
266 /* not implemented */
267}
268
269void
270audit_session_close(const char *ttyn)
271{
272 /* not implemented */
273}
274
275void
276audit_event(ssh_audit_event_t event)
277{
278 char textbuf[BSM_TEXTBUFSZ];
279 static int logged_in = 0;
280 const char *user = the_authctxt ? the_authctxt->user : "(unknown user)";
281
282 if (cannot_audit(0))
283 return;
284
285 switch(event) {
286 case SSH_AUTH_SUCCESS:
287 logged_in = 1;
288 bsm_audit_session_setup();
289 snprintf(textbuf, sizeof(textbuf),
290 gettext("successful login %s"), user);
291 bsm_audit_record(0, textbuf, AUE_openssh);
292 break;
293
294 case SSH_CONNECTION_CLOSE:
295 /*
296 * We can also get a close event if the user attempted auth
297 * but never succeeded.
298 */
299 if (logged_in) {
300 snprintf(textbuf, sizeof(textbuf),
301 gettext("sshd logout %s"), the_authctxt->user);
302 bsm_audit_record(0, textbuf, AUE_logout);
303 } else {
304 debug("%s: connection closed without authentication",
305 __func__);
306 }
307 break;
308
309 case SSH_NOLOGIN:
310 bsm_audit_record(1,
311 gettext("logins disabled by /etc/nologin"), AUE_openssh);
312 break;
313
314 case SSH_LOGIN_EXCEED_MAXTRIES:
315 snprintf(textbuf, sizeof(textbuf),
316 gettext("too many tries for user %s"), the_authctxt->user);
317 bsm_audit_record(1, textbuf, AUE_openssh);
318 break;
319
320 case SSH_LOGIN_ROOT_DENIED:
321 bsm_audit_record(2, gettext("not_console"), AUE_openssh);
322 break;
323
324 case SSH_AUTH_FAIL_PASSWD:
325 bsm_audit_bad_login("password");
326 break;
327
328 case SSH_AUTH_FAIL_KBDINT:
329 bsm_audit_bad_login("interactive password entry");
330 break;
331
332 default:
333 debug("%s: unhandled event %d", __func__, event);
334 }
335}
336#endif /* BSM */
2
3/*
4 * TODO
5 *
6 * - deal with overlap between this and sys_auth_allowed_user
7 * sys_auth_record_login and record_failed_login.
8 */
9
10/*
11 * Copyright 1988-2002 Sun Microsystems, Inc. All rights reserved.
12 * Use is subject to license terms.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
35/* #pragma ident "@(#)bsmaudit.c 1.1 01/09/17 SMI" */
36
37#include "includes.h"
38#if defined(USE_BSM_AUDIT)
39
40#include <sys/types.h>
41
42#include <stdarg.h>
43#include <unistd.h>
44
45#include "ssh.h"
46#include "log.h"
47#include "key.h"
48#include "hostfile.h"
49#include "auth.h"
50#include "xmalloc.h"
51
52#ifndef AUE_openssh
53# define AUE_openssh 32800
54#endif
55#include <bsm/audit.h>
56#include <bsm/libbsm.h>
57#include <bsm/audit_uevents.h>
58#include <bsm/audit_record.h>
59#include <locale.h>
60
61#if defined(HAVE_GETAUDIT_ADDR)
62#define AuditInfoStruct auditinfo_addr
63#define AuditInfoTermID au_tid_addr_t
64#define GetAuditFunc(a,b) getaudit_addr((a),(b))
65#define GetAuditFuncText "getaudit_addr"
66#define SetAuditFunc(a,b) setaudit_addr((a),(b))
67#define SetAuditFuncText "setaudit_addr"
68#define AUToSubjectFunc au_to_subject_ex
69#define AUToReturnFunc(a,b) au_to_return32((a), (int32_t)(b))
70#else
71#define AuditInfoStruct auditinfo
72#define AuditInfoTermID au_tid_t
73#define GetAuditFunc(a,b) getaudit(a)
74#define GetAuditFuncText "getaudit"
75#define SetAuditFunc(a,b) setaudit(a)
76#define SetAuditFuncText "setaudit"
77#define AUToSubjectFunc au_to_subject
78#define AUToReturnFunc(a,b) au_to_return((a), (u_int)(b))
79#endif
80
81extern int cannot_audit(int);
82extern void aug_init(void);
83extern dev_t aug_get_port(void);
84extern int aug_get_machine(char *, u_int32_t *, u_int32_t *);
85extern void aug_save_auid(au_id_t);
86extern void aug_save_uid(uid_t);
87extern void aug_save_euid(uid_t);
88extern void aug_save_gid(gid_t);
89extern void aug_save_egid(gid_t);
90extern void aug_save_pid(pid_t);
91extern void aug_save_asid(au_asid_t);
92extern void aug_save_tid(dev_t, unsigned int);
93extern void aug_save_tid_ex(dev_t, u_int32_t *, u_int32_t);
94extern int aug_save_me(void);
95extern int aug_save_namask(void);
96extern void aug_save_event(au_event_t);
97extern void aug_save_sorf(int);
98extern void aug_save_text(char *);
99extern void aug_save_text1(char *);
100extern void aug_save_text2(char *);
101extern void aug_save_na(int);
102extern void aug_save_user(char *);
103extern void aug_save_path(char *);
104extern int aug_save_policy(void);
105extern void aug_save_afunc(int (*)(int));
106extern int aug_audit(void);
107extern int aug_na_selected(void);
108extern int aug_selected(void);
109extern int aug_daemon_session(void);
110
111#ifndef HAVE_GETTEXT
112# define gettext(a) (a)
113#endif
114
115extern Authctxt *the_authctxt;
116static AuditInfoTermID ssh_bsm_tid;
117
118/* Below is the low-level BSM interface code */
119
120/*
121 * Check if the specified event is selected (enabled) for auditing.
122 * Returns 1 if the event is selected, 0 if not and -1 on failure.
123 */
124static int
125selected(char *username, uid_t uid, au_event_t event, int sf)
126{
127 int rc, sorf;
128 char naflags[512];
129 struct au_mask mask;
130
131 mask.am_success = mask.am_failure = 0;
132 if (uid < 0) {
133 /* get flags for non-attributable (to a real user) events */
134 rc = getacna(naflags, sizeof(naflags));
135 if (rc == 0)
136 (void) getauditflagsbin(naflags, &mask);
137 } else
138 rc = au_user_mask(username, &mask);
139
140 sorf = (sf == 0) ? AU_PRS_SUCCESS : AU_PRS_FAILURE;
141 return(au_preselect(event, &mask, sorf, AU_PRS_REREAD));
142}
143
144static void
145bsm_audit_record(int typ, char *string, au_event_t event_no)
146{
147 int ad, rc, sel;
148 uid_t uid = -1;
149 gid_t gid = -1;
150 pid_t pid = getpid();
151 AuditInfoTermID tid = ssh_bsm_tid;
152
153 if (the_authctxt != NULL && the_authctxt->valid) {
154 uid = the_authctxt->pw->pw_uid;
155 gid = the_authctxt->pw->pw_gid;
156 }
157
158 rc = (typ == 0) ? 0 : -1;
159 sel = selected(the_authctxt->user, uid, event_no, rc);
160 debug3("BSM audit: typ %d rc %d \"%s\"", typ, rc, string);
161 if (!sel)
162 return; /* audit event does not match mask, do not write */
163
164 debug3("BSM audit: writing audit new record");
165 ad = au_open();
166
167 (void) au_write(ad, AUToSubjectFunc(uid, uid, gid, uid, gid,
168 pid, pid, &tid));
169 (void) au_write(ad, au_to_text(string));
170 (void) au_write(ad, AUToReturnFunc(typ, rc));
171
172 rc = au_close(ad, AU_TO_WRITE, event_no);
173 if (rc < 0)
174 error("BSM audit: %s failed to write \"%s\" record: %s",
175 __func__, string, strerror(errno));
176}
177
178static void
179bsm_audit_session_setup(void)
180{
181 int rc;
182 struct AuditInfoStruct info;
183 au_mask_t mask;
184
185 if (the_authctxt == NULL) {
186 error("BSM audit: session setup internal error (NULL ctxt)");
187 return;
188 }
189
190 if (the_authctxt->valid)
191 info.ai_auid = the_authctxt->pw->pw_uid;
192 else
193 info.ai_auid = -1;
194 info.ai_asid = getpid();
195 mask.am_success = 0;
196 mask.am_failure = 0;
197
198 (void) au_user_mask(the_authctxt->user, &mask);
199
200 info.ai_mask.am_success = mask.am_success;
201 info.ai_mask.am_failure = mask.am_failure;
202
203 info.ai_termid = ssh_bsm_tid;
204
205 rc = SetAuditFunc(&info, sizeof(info));
206 if (rc < 0)
207 error("BSM audit: %s: %s failed: %s", __func__,
208 SetAuditFuncText, strerror(errno));
209}
210
211static void
212bsm_audit_bad_login(const char *what)
213{
214 char textbuf[BSM_TEXTBUFSZ];
215
216 if (the_authctxt->valid) {
217 (void) snprintf(textbuf, sizeof (textbuf),
218 gettext("invalid %s for user %s"),
219 what, the_authctxt->user);
220 bsm_audit_record(4, textbuf, AUE_openssh);
221 } else {
222 (void) snprintf(textbuf, sizeof (textbuf),
223 gettext("invalid user name \"%s\""),
224 the_authctxt->user);
225 bsm_audit_record(3, textbuf, AUE_openssh);
226 }
227}
228
229/* Below is the sshd audit API code */
230
231void
232audit_connection_from(const char *host, int port)
233{
234 AuditInfoTermID *tid = &ssh_bsm_tid;
235 char buf[1024];
236
237 if (cannot_audit(0))
238 return;
239 debug3("BSM audit: connection from %.100s port %d", host, port);
240
241 /* populate our terminal id structure */
242#if defined(HAVE_GETAUDIT_ADDR)
243 tid->at_port = (dev_t)port;
244 aug_get_machine((char *)host, &(tid->at_addr[0]), &(tid->at_type));
245 snprintf(buf, sizeof(buf), "%08x %08x %08x %08x", tid->at_addr[0],
246 tid->at_addr[1], tid->at_addr[2], tid->at_addr[3]);
247 debug3("BSM audit: iptype %d machine ID %s", (int)tid->at_type, buf);
248#else
249 /* this is used on IPv4-only machines */
250 tid->port = (dev_t)port;
251 tid->machine = inet_addr(host);
252 snprintf(buf, sizeof(buf), "%08x", tid->machine);
253 debug3("BSM audit: machine ID %s", buf);
254#endif
255}
256
257void
258audit_run_command(const char *command)
259{
260 /* not implemented */
261}
262
263void
264audit_session_open(const char *ttyn)
265{
266 /* not implemented */
267}
268
269void
270audit_session_close(const char *ttyn)
271{
272 /* not implemented */
273}
274
275void
276audit_event(ssh_audit_event_t event)
277{
278 char textbuf[BSM_TEXTBUFSZ];
279 static int logged_in = 0;
280 const char *user = the_authctxt ? the_authctxt->user : "(unknown user)";
281
282 if (cannot_audit(0))
283 return;
284
285 switch(event) {
286 case SSH_AUTH_SUCCESS:
287 logged_in = 1;
288 bsm_audit_session_setup();
289 snprintf(textbuf, sizeof(textbuf),
290 gettext("successful login %s"), user);
291 bsm_audit_record(0, textbuf, AUE_openssh);
292 break;
293
294 case SSH_CONNECTION_CLOSE:
295 /*
296 * We can also get a close event if the user attempted auth
297 * but never succeeded.
298 */
299 if (logged_in) {
300 snprintf(textbuf, sizeof(textbuf),
301 gettext("sshd logout %s"), the_authctxt->user);
302 bsm_audit_record(0, textbuf, AUE_logout);
303 } else {
304 debug("%s: connection closed without authentication",
305 __func__);
306 }
307 break;
308
309 case SSH_NOLOGIN:
310 bsm_audit_record(1,
311 gettext("logins disabled by /etc/nologin"), AUE_openssh);
312 break;
313
314 case SSH_LOGIN_EXCEED_MAXTRIES:
315 snprintf(textbuf, sizeof(textbuf),
316 gettext("too many tries for user %s"), the_authctxt->user);
317 bsm_audit_record(1, textbuf, AUE_openssh);
318 break;
319
320 case SSH_LOGIN_ROOT_DENIED:
321 bsm_audit_record(2, gettext("not_console"), AUE_openssh);
322 break;
323
324 case SSH_AUTH_FAIL_PASSWD:
325 bsm_audit_bad_login("password");
326 break;
327
328 case SSH_AUTH_FAIL_KBDINT:
329 bsm_audit_bad_login("interactive password entry");
330 break;
331
332 default:
333 debug("%s: unhandled event %d", __func__, event);
334 }
335}
336#endif /* BSM */