4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
37 #include <bsm/adt_event.h>
38 #include "login_audit.h"
41 * Key assumption: login is single threaded.
43 static void audit_logout(adt_session_data_t
*);
46 * if audit is not enabled, the adt_*() functions simply return without
47 * doing anything. In the success case, the credential has already been
48 * setup with audit data by PAM.
52 * There is no information passed to login.c from telnet
53 * about the terminal id. They both set the tid before they
54 * exec login; the value is picked up by adt_start_session() and is
55 * carefully *not* overwritten by adt_load_hostname().
59 audit_success(uint_t event_id
, struct passwd
*pwd
, char *optional_text
)
61 adt_session_data_t
*ah
;
62 adt_event_data_t
*event
;
67 if (adt_start_session(&ah
, NULL
, ADT_USE_PROC_DATA
)) {
68 syslog(LOG_AUTH
| LOG_ALERT
, "login adt_start_session(): %m");
71 if (adt_set_user(ah
, pwd
->pw_uid
, pwd
->pw_gid
,
72 pwd
->pw_uid
, pwd
->pw_gid
, NULL
, ADT_USER
)) {
73 syslog(LOG_AUTH
| LOG_ALERT
, "login adt_set_user(): %m");
74 (void) adt_end_session(ah
);
77 event
= adt_alloc_event(ah
, event_id
);
84 event
->adt_zlogin
.message
= optional_text
;
89 rc
= adt_put_event(event
, ADT_SUCCESS
, ADT_SUCCESS
);
91 (void) adt_free_event(event
);
93 (void) adt_end_session(ah
);
94 syslog(LOG_AUTH
| LOG_ALERT
, "login adt_put_event(): %m");
98 * The code above executes whether or not audit is enabled.
99 * However audit_logout must only execute if audit is
100 * enabled so we don't fork unnecessarily.
102 if (adt_audit_enabled()) {
107 audit_logout(ah
); /* fork to catch logout */
111 (void) adt_end_session(ah
);
115 * errors are ignored since there is no action to take on error
118 audit_logout(adt_session_data_t
*ah
)
120 adt_event_data_t
*logout
;
121 int status
; /* wait status */
123 priv_set_t
*priv
; /* waiting process privs */
125 if ((logout
= adt_alloc_event(ah
, ADT_logout
)) == NULL
) {
126 syslog(LOG_AUTH
| LOG_ALERT
,
127 "adt_alloc_event(ADT_logout): %m");
130 if ((priv
= priv_allocset()) == NULL
) {
131 syslog(LOG_AUTH
| LOG_ALERT
,
132 "login audit_logout: could not alloc basic privs: %m");
133 adt_free_event(logout
);
138 * The child returns and continues the login processing.
139 * The parent's sole job is to wait for child exit, write the
140 * logout audit record, and replay the child's exit code.
143 if ((pid
= fork()) == 0) {
146 adt_free_event(logout
);
153 syslog(LOG_AUTH
| LOG_ALERT
,
154 "login audit_logout: could not fork: %m");
155 adt_free_event(logout
);
163 * When this routine is called, the current working
164 * directory is the user's home directory and there are
165 * unknown open files. For the waiting process, change the
166 * current directory to root and close files so that the
167 * user's home directory and anything else can be unmounted
170 if (chdir("/") != 0) {
171 syslog(LOG_AUTH
| LOG_ALERT
,
172 "login audit_logut: could not chdir /: %m");
175 * Reduce privileges to just those needed.
178 (void) priv_delset(priv
, PRIV_PROC_EXEC
);
179 (void) priv_delset(priv
, PRIV_PROC_FORK
);
180 (void) priv_delset(priv
, PRIV_PROC_INFO
);
181 (void) priv_delset(priv
, PRIV_PROC_SESSION
);
182 (void) priv_delset(priv
, PRIV_FILE_LINK_ANY
);
183 if ((priv_addset(priv
, PRIV_PROC_AUDIT
) != 0) ||
184 (setppriv(PRIV_SET
, PRIV_PERMITTED
, priv
) != 0)) {
185 syslog(LOG_AUTH
| LOG_ALERT
,
186 "login audit_logout: could not reduce privs: %m");
190 while (pid
!= waitpid(pid
, &status
, 0))
193 (void) adt_put_event(logout
, ADT_SUCCESS
, ADT_SUCCESS
);
194 adt_free_event(logout
);
195 (void) adt_end_session(ah
);
196 exit(WEXITSTATUS(status
));
200 * errors are ignored since there is no action to take on error.
202 * If the user id is invalid, pwd is NULL.
205 audit_failure(uint_t event_id
, int failure_code
, struct passwd
*pwd
,
206 const char *hostname
, const char *ttyname
, char *optional_text
)
208 adt_session_data_t
*ah
;
209 adt_event_data_t
*event
;
214 if (adt_start_session(&ah
, NULL
, ADT_USE_PROC_DATA
))
224 * If this is a remote login, in.telnetd has
225 * already set the terminal id, in which case
226 * adt_load_hostname() will use the preset terminal id and
227 * ignore hostname. (If no remote host and ttyname is NULL,
228 * let adt_load_ttyname() figure out what to do.)
230 if (*hostname
== '\0')
231 (void) adt_load_ttyname(ttyname
, &p_tid
);
233 (void) adt_load_hostname(hostname
, &p_tid
);
235 if (adt_set_user(ah
, uid
, gid
, uid
, gid
, p_tid
, ADT_NEW
)) {
236 (void) adt_end_session(ah
);
242 event
= adt_alloc_event(ah
, event_id
);
248 event
->adt_zlogin
.message
= optional_text
;
251 (void) adt_put_event(event
, ADT_FAILURE
, failure_code
);
253 adt_free_event(event
);
254 (void) adt_end_session(ah
);