1 /* $Id: audit-bsm.c,v 1.8 2012/02/23 23:40:43 dtucker Exp $ */
6 * - deal with overlap between this and sys_auth_allowed_user
7 * sys_auth_record_login and record_failed_login.
11 * Copyright 1988-2002 Sun Microsystems, Inc. All rights reserved.
12 * Use is subject to license terms.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
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.
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.
35 /* #pragma ident "@(#)bsmaudit.c 1.1 01/09/17 SMI" */
38 #if defined(USE_BSM_AUDIT)
40 #include <sys/types.h>
60 # define AUE_openssh 32800
62 #include <bsm/audit.h>
63 #include <bsm/libbsm.h>
64 #include <bsm/audit_uevents.h>
65 #include <bsm/audit_record.h>
68 #if defined(HAVE_GETAUDIT_ADDR)
69 #define AuditInfoStruct auditinfo_addr
70 #define AuditInfoTermID au_tid_addr_t
71 #define SetAuditFunc(a,b) setaudit_addr((a),(b))
72 #define SetAuditFuncText "setaudit_addr"
73 #define AUToSubjectFunc au_to_subject_ex
74 #define AUToReturnFunc(a,b) au_to_return32((a), (int32_t)(b))
76 #define AuditInfoStruct auditinfo
77 #define AuditInfoTermID au_tid_t
78 #define SetAuditFunc(a,b) setaudit(a)
79 #define SetAuditFuncText "setaudit"
80 #define AUToSubjectFunc au_to_subject
81 #define AUToReturnFunc(a,b) au_to_return((a), (u_int)(b))
85 extern int cannot_audit(int);
87 extern void aug_init(void);
88 extern void aug_save_auid(au_id_t
);
89 extern void aug_save_uid(uid_t
);
90 extern void aug_save_euid(uid_t
);
91 extern void aug_save_gid(gid_t
);
92 extern void aug_save_egid(gid_t
);
93 extern void aug_save_pid(pid_t
);
94 extern void aug_save_asid(au_asid_t
);
95 extern void aug_save_tid(dev_t
, unsigned int);
96 extern void aug_save_tid_ex(dev_t
, u_int32_t
*, u_int32_t
);
97 extern int aug_save_me(void);
98 extern int aug_save_namask(void);
99 extern void aug_save_event(au_event_t
);
100 extern void aug_save_sorf(int);
101 extern void aug_save_text(char *);
102 extern void aug_save_text1(char *);
103 extern void aug_save_text2(char *);
104 extern void aug_save_na(int);
105 extern void aug_save_user(char *);
106 extern void aug_save_path(char *);
107 extern int aug_save_policy(void);
108 extern void aug_save_afunc(int (*)(int));
109 extern int aug_audit(void);
110 extern int aug_na_selected(void);
111 extern int aug_selected(void);
112 extern int aug_daemon_session(void);
115 # define gettext(a) (a)
118 extern Authctxt
*the_authctxt
;
119 static AuditInfoTermID ssh_bsm_tid
;
121 #ifdef BROKEN_BSM_API
122 /* For some reason this constant is no longer defined
124 #define BSM_TEXTBUFSZ 256
127 /* Below is the low-level BSM interface code */
130 * aug_get_machine is only required on IPv6 capable machines, we use a
131 * different mechanism in audit_connection_from() for IPv4-only machines.
132 * getaudit_addr() is only present on IPv6 capable machines.
134 #if defined(HAVE_AUG_GET_MACHINE) || !defined(HAVE_GETAUDIT_ADDR)
135 extern int aug_get_machine(char *, u_int32_t
*, u_int32_t
*);
138 aug_get_machine(char *host
, u_int32_t
*addr
, u_int32_t
*type
)
141 struct sockaddr_in
*in4
;
142 struct sockaddr_in6
*in6
;
145 if ((r
= getaddrinfo(host
, NULL
, NULL
, &ai
)) != 0) {
146 error("BSM audit: getaddrinfo failed for %.100s: %.100s", host
,
147 r
== EAI_SYSTEM
? strerror(errno
) : gai_strerror(r
));
151 switch (ai
->ai_family
) {
153 in4
= (struct sockaddr_in
*)ai
->ai_addr
;
155 memcpy(addr
, &in4
->sin_addr
, sizeof(struct in_addr
));
159 in6
= (struct sockaddr_in6
*)ai
->ai_addr
;
161 memcpy(addr
, &in6
->sin6_addr
, sizeof(struct in6_addr
));
165 error("BSM audit: unknown address family for %.100s: %d",
166 host
, ai
->ai_family
);
174 #ifdef BROKEN_BSM_API
176 In Solaris 11 the audit daemon has been moved to SMF. In the process
177 they simply dropped getacna() from the API, since it read from a now
178 non-existent config file. This function re-implements getacna() to
179 read from the SMF repository instead.
182 getacna(char *auditstring
, int len
)
184 scf_handle_t
*handle
= NULL
;
185 scf_property_t
*property
= NULL
;
186 scf_value_t
*value
= NULL
;
189 handle
= scf_handle_create(SCF_VERSION
);
191 return -2; /* The man page for getacna on Solaris 10 states
192 we should return -2 in case of error and set
193 errno to indicate the error. We don't bother
194 with errno here, though, since the only use
195 of this function below doesn't check for errors
199 ret
= scf_handle_bind(handle
);
203 property
= scf_property_create(handle
);
204 if (property
== NULL
)
207 ret
= scf_handle_decode_fmri(handle
,
208 "svc:/system/auditd:default/:properties/preselection/naflags",
209 NULL
, NULL
, NULL
, NULL
, property
, 0);
213 value
= scf_value_create(handle
);
217 ret
= scf_property_get_value(property
, value
);
221 ret
= scf_value_get_astring(value
, auditstring
, len
);
225 scf_value_destroy(value
);
226 scf_property_destroy(property
);
227 scf_handle_destroy(handle
);
234 * Check if the specified event is selected (enabled) for auditing.
235 * Returns 1 if the event is selected, 0 if not and -1 on failure.
238 selected(char *username
, uid_t uid
, au_event_t event
, int sf
)
244 mask
.am_success
= mask
.am_failure
= 0;
246 /* get flags for non-attributable (to a real user) events */
247 rc
= getacna(naflags
, sizeof(naflags
));
249 (void) getauditflagsbin(naflags
, &mask
);
251 rc
= au_user_mask(username
, &mask
);
253 sorf
= (sf
== 0) ? AU_PRS_SUCCESS
: AU_PRS_FAILURE
;
254 return(au_preselect(event
, &mask
, sorf
, AU_PRS_REREAD
));
258 bsm_audit_record(int typ
, char *string
, au_event_t event_no
)
263 pid_t pid
= getpid();
264 AuditInfoTermID tid
= ssh_bsm_tid
;
266 if (the_authctxt
!= NULL
&& the_authctxt
->valid
) {
267 uid
= the_authctxt
->pw
->pw_uid
;
268 gid
= the_authctxt
->pw
->pw_gid
;
271 rc
= (typ
== 0) ? 0 : -1;
272 sel
= selected(the_authctxt
->user
, uid
, event_no
, rc
);
273 debug3("BSM audit: typ %d rc %d \"%s\"", typ
, rc
, string
);
275 return; /* audit event does not match mask, do not write */
277 debug3("BSM audit: writing audit new record");
280 (void) au_write(ad
, AUToSubjectFunc(uid
, uid
, gid
, uid
, gid
,
282 (void) au_write(ad
, au_to_text(string
));
283 (void) au_write(ad
, AUToReturnFunc(typ
, rc
));
285 #ifdef BROKEN_BSM_API
286 /* The last argument is the event modifier flags. For
287 some seemingly undocumented reason it was added in
289 rc
= au_close(ad
, AU_TO_WRITE
, event_no
, 0);
291 rc
= au_close(ad
, AU_TO_WRITE
, event_no
);
295 error("BSM audit: %s failed to write \"%s\" record: %s",
296 __func__
, string
, strerror(errno
));
300 bsm_audit_session_setup(void)
303 struct AuditInfoStruct info
;
306 if (the_authctxt
== NULL
) {
307 error("BSM audit: session setup internal error (NULL ctxt)");
311 if (the_authctxt
->valid
)
312 info
.ai_auid
= the_authctxt
->pw
->pw_uid
;
315 info
.ai_asid
= getpid();
319 (void) au_user_mask(the_authctxt
->user
, &mask
);
321 info
.ai_mask
.am_success
= mask
.am_success
;
322 info
.ai_mask
.am_failure
= mask
.am_failure
;
324 info
.ai_termid
= ssh_bsm_tid
;
326 rc
= SetAuditFunc(&info
, sizeof(info
));
328 error("BSM audit: %s: %s failed: %s", __func__
,
329 SetAuditFuncText
, strerror(errno
));
333 bsm_audit_bad_login(const char *what
)
335 char textbuf
[BSM_TEXTBUFSZ
];
337 if (the_authctxt
->valid
) {
338 (void) snprintf(textbuf
, sizeof (textbuf
),
339 gettext("invalid %s for user %s"),
340 what
, the_authctxt
->user
);
341 bsm_audit_record(4, textbuf
, AUE_openssh
);
343 (void) snprintf(textbuf
, sizeof (textbuf
),
344 gettext("invalid user name \"%s\""),
346 bsm_audit_record(3, textbuf
, AUE_openssh
);
350 /* Below is the sshd audit API code */
353 audit_connection_from(const char *host
, int port
)
355 AuditInfoTermID
*tid
= &ssh_bsm_tid
;
360 debug3("BSM audit: connection from %.100s port %d", host
, port
);
362 /* populate our terminal id structure */
363 #if defined(HAVE_GETAUDIT_ADDR)
364 tid
->at_port
= (dev_t
)port
;
365 aug_get_machine((char *)host
, &(tid
->at_addr
[0]), &(tid
->at_type
));
366 snprintf(buf
, sizeof(buf
), "%08x %08x %08x %08x", tid
->at_addr
[0],
367 tid
->at_addr
[1], tid
->at_addr
[2], tid
->at_addr
[3]);
368 debug3("BSM audit: iptype %d machine ID %s", (int)tid
->at_type
, buf
);
370 /* this is used on IPv4-only machines */
371 tid
->port
= (dev_t
)port
;
372 tid
->machine
= inet_addr(host
);
373 snprintf(buf
, sizeof(buf
), "%08x", tid
->machine
);
374 debug3("BSM audit: machine ID %s", buf
);
379 audit_run_command(const char *command
)
381 /* not implemented */
385 audit_session_open(struct logininfo
*li
)
387 /* not implemented */
391 audit_session_close(struct logininfo
*li
)
393 /* not implemented */
397 audit_event(ssh_audit_event_t event
)
399 char textbuf
[BSM_TEXTBUFSZ
];
400 static int logged_in
= 0;
401 const char *user
= the_authctxt
? the_authctxt
->user
: "(unknown user)";
407 case SSH_AUTH_SUCCESS
:
409 bsm_audit_session_setup();
410 snprintf(textbuf
, sizeof(textbuf
),
411 gettext("successful login %s"), user
);
412 bsm_audit_record(0, textbuf
, AUE_openssh
);
415 case SSH_CONNECTION_CLOSE
:
417 * We can also get a close event if the user attempted auth
418 * but never succeeded.
421 snprintf(textbuf
, sizeof(textbuf
),
422 gettext("sshd logout %s"), the_authctxt
->user
);
423 bsm_audit_record(0, textbuf
, AUE_logout
);
425 debug("%s: connection closed without authentication",
432 gettext("logins disabled by /etc/nologin"), AUE_openssh
);
435 case SSH_LOGIN_EXCEED_MAXTRIES
:
436 snprintf(textbuf
, sizeof(textbuf
),
437 gettext("too many tries for user %s"), the_authctxt
->user
);
438 bsm_audit_record(1, textbuf
, AUE_openssh
);
441 case SSH_LOGIN_ROOT_DENIED
:
442 bsm_audit_record(2, gettext("not_console"), AUE_openssh
);
445 case SSH_AUTH_FAIL_PASSWD
:
446 bsm_audit_bad_login("password");
449 case SSH_AUTH_FAIL_KBDINT
:
450 bsm_audit_bad_login("interactive password entry");
454 debug("%s: unhandled event %d", __func__
, event
);