libuutil: move under bmake
[unleashed.git] / usr / src / cmd / login / login_audit.c
blobe8b1208175aae28d35fd9f89c34967d4852268ac
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <assert.h>
27 #include <priv.h>
28 #include <pwd.h>
29 #include <signal.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <syslog.h>
33 #include <unistd.h>
34 #include <sys/wait.h>
36 #include <bsm/adt.h>
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().
58 void
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;
63 int rc;
65 assert(pwd != NULL);
67 if (adt_start_session(&ah, NULL, ADT_USE_PROC_DATA)) {
68 syslog(LOG_AUTH | LOG_ALERT, "login adt_start_session(): %m");
69 return;
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);
75 return;
77 event = adt_alloc_event(ah, event_id);
79 if (event == NULL)
80 return;
82 switch (event_id) {
83 case ADT_zlogin:
84 event->adt_zlogin.message = optional_text;
85 break;
86 default:
87 break;
89 rc = adt_put_event(event, ADT_SUCCESS, ADT_SUCCESS);
91 (void) adt_free_event(event);
92 if (rc) {
93 (void) adt_end_session(ah);
94 syslog(LOG_AUTH | LOG_ALERT, "login adt_put_event(): %m");
95 return;
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()) {
103 switch (event_id) {
104 case ADT_login:
105 case ADT_telnet:
106 case ADT_zlogin:
107 audit_logout(ah); /* fork to catch logout */
108 break;
111 (void) adt_end_session(ah);
115 * errors are ignored since there is no action to take on error
117 static void
118 audit_logout(adt_session_data_t *ah)
120 adt_event_data_t *logout;
121 int status; /* wait status */
122 pid_t pid;
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");
128 return;
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);
134 return;
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) {
144 /* child */
146 adt_free_event(logout);
147 priv_freeset(priv);
148 return;
150 if (pid == -1) {
151 /* failure */
153 syslog(LOG_AUTH | LOG_ALERT,
154 "login audit_logout: could not fork: %m");
155 adt_free_event(logout);
156 priv_freeset(priv);
157 return;
160 /* parent process */
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
168 * if necessary.
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.
177 priv_basicset(priv);
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");
188 closefrom(0);
189 priv_freeset(priv);
190 while (pid != waitpid(pid, &status, 0))
191 continue;
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.
204 void
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;
210 uid_t uid;
211 gid_t gid;
212 adt_termid_t *p_tid;
214 if (adt_start_session(&ah, NULL, ADT_USE_PROC_DATA))
215 return;
217 uid = ADT_NO_ATTRIB;
218 gid = ADT_NO_ATTRIB;
219 if (pwd != NULL) {
220 uid = pwd->pw_uid;
221 gid = pwd->pw_gid;
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);
232 else
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);
237 free(p_tid);
238 return;
240 free(p_tid);
242 event = adt_alloc_event(ah, event_id);
243 if (event == NULL) {
244 return;
246 switch (event_id) {
247 case ADT_zlogin:
248 event->adt_zlogin.message = optional_text;
249 break;
251 (void) adt_put_event(event, ADT_FAILURE, failure_code);
253 adt_free_event(event);
254 (void) adt_end_session(ah);