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 rlogin or 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()) {
108 audit_logout(ah
); /* fork to catch logout */
112 (void) adt_end_session(ah
);
116 * errors are ignored since there is no action to take on error
119 audit_logout(adt_session_data_t
*ah
)
121 adt_event_data_t
*logout
;
122 int status
; /* wait status */
124 priv_set_t
*priv
; /* waiting process privs */
126 if ((logout
= adt_alloc_event(ah
, ADT_logout
)) == NULL
) {
127 syslog(LOG_AUTH
| LOG_ALERT
,
128 "adt_alloc_event(ADT_logout): %m");
131 if ((priv
= priv_allocset()) == NULL
) {
132 syslog(LOG_AUTH
| LOG_ALERT
,
133 "login audit_logout: could not alloc basic privs: %m");
134 adt_free_event(logout
);
139 * The child returns and continues the login processing.
140 * The parent's sole job is to wait for child exit, write the
141 * logout audit record, and replay the child's exit code.
144 if ((pid
= fork()) == 0) {
147 adt_free_event(logout
);
154 syslog(LOG_AUTH
| LOG_ALERT
,
155 "login audit_logout: could not fork: %m");
156 adt_free_event(logout
);
164 * When this routine is called, the current working
165 * directory is the user's home directory and there are
166 * unknown open files. For the waiting process, change the
167 * current directory to root and close files so that the
168 * user's home directory and anything else can be unmounted
171 if (chdir("/") != 0) {
172 syslog(LOG_AUTH
| LOG_ALERT
,
173 "login audit_logut: could not chdir /: %m");
176 * Reduce privileges to just those needed.
179 (void) priv_delset(priv
, PRIV_PROC_EXEC
);
180 (void) priv_delset(priv
, PRIV_PROC_FORK
);
181 (void) priv_delset(priv
, PRIV_PROC_INFO
);
182 (void) priv_delset(priv
, PRIV_PROC_SESSION
);
183 (void) priv_delset(priv
, PRIV_FILE_LINK_ANY
);
184 if ((priv_addset(priv
, PRIV_PROC_AUDIT
) != 0) ||
185 (setppriv(PRIV_SET
, PRIV_PERMITTED
, priv
) != 0)) {
186 syslog(LOG_AUTH
| LOG_ALERT
,
187 "login audit_logout: could not reduce privs: %m");
191 while (pid
!= waitpid(pid
, &status
, 0))
194 (void) adt_put_event(logout
, ADT_SUCCESS
, ADT_SUCCESS
);
195 adt_free_event(logout
);
196 (void) adt_end_session(ah
);
197 exit(WEXITSTATUS(status
));
201 * errors are ignored since there is no action to take on error.
203 * If the user id is invalid, pwd is NULL.
206 audit_failure(uint_t event_id
, int failure_code
, struct passwd
*pwd
,
207 const char *hostname
, const char *ttyname
, char *optional_text
)
209 adt_session_data_t
*ah
;
210 adt_event_data_t
*event
;
215 if (adt_start_session(&ah
, NULL
, ADT_USE_PROC_DATA
))
225 * If this is a remote login, in.rlogind or in.telnetd has
226 * already set the terminal id, in which case
227 * adt_load_hostname() will use the preset terminal id and
228 * ignore hostname. (If no remote host and ttyname is NULL,
229 * let adt_load_ttyname() figure out what to do.)
231 if (*hostname
== '\0')
232 (void) adt_load_ttyname(ttyname
, &p_tid
);
234 (void) adt_load_hostname(hostname
, &p_tid
);
236 if (adt_set_user(ah
, uid
, gid
, uid
, gid
, p_tid
, ADT_NEW
)) {
237 (void) adt_end_session(ah
);
245 event
= adt_alloc_event(ah
, event_id
);
251 event
->adt_zlogin
.message
= optional_text
;
254 (void) adt_put_event(event
, ADT_FAILURE
, failure_code
);
256 adt_free_event(event
);
257 (void) adt_end_session(ah
);