xref: /titanic_50/usr/src/cmd/ssh/sshd/bsmaudit.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  *
22*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
23*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24*7c478bd9Sstevel@tonic-gate  *
25*7c478bd9Sstevel@tonic-gate  * usr/src/cmd/ssh/sshd/bsmaudit.c
26*7c478bd9Sstevel@tonic-gate  *
27*7c478bd9Sstevel@tonic-gate  * Taken from the on81 usr/src/lib/libbsm/common/audit_login.c
28*7c478bd9Sstevel@tonic-gate  */
29*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #include "includes.h"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/wait.h>
40*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
41*7c478bd9Sstevel@tonic-gate #include <netdb.h>
42*7c478bd9Sstevel@tonic-gate #include <signal.h>
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
45*7c478bd9Sstevel@tonic-gate #include <pwd.h>
46*7c478bd9Sstevel@tonic-gate #include <shadow.h>
47*7c478bd9Sstevel@tonic-gate #include <utmpx.h>
48*7c478bd9Sstevel@tonic-gate #include <unistd.h>
49*7c478bd9Sstevel@tonic-gate #include <string.h>
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate #include <locale.h>
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate #include "log.h"
54*7c478bd9Sstevel@tonic-gate #include "packet.h"
55*7c478bd9Sstevel@tonic-gate #include "canohost.h"
56*7c478bd9Sstevel@tonic-gate #include "servconf.h"
57*7c478bd9Sstevel@tonic-gate #include <errno.h>
58*7c478bd9Sstevel@tonic-gate #include <bsm/adt.h>
59*7c478bd9Sstevel@tonic-gate #include <bsm/adt_event.h>
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate extern uint_t utmp_len; /* XXX - Yuck; we'll keep this for now */
62*7c478bd9Sstevel@tonic-gate extern ServerOptions options;
63*7c478bd9Sstevel@tonic-gate 	/*
64*7c478bd9Sstevel@tonic-gate 	 * XXX - Yuck; we should have a
65*7c478bd9Sstevel@tonic-gate 	 * get_client_name_or_ip that does the
66*7c478bd9Sstevel@tonic-gate 	 * right thing wrt reverse lookups
67*7c478bd9Sstevel@tonic-gate 	 */
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate void
70*7c478bd9Sstevel@tonic-gate audit_sshd_chauthtok(int pam_retval, uid_t uid, gid_t gid)
71*7c478bd9Sstevel@tonic-gate {
72*7c478bd9Sstevel@tonic-gate 	adt_session_data_t	*ah	= NULL;
73*7c478bd9Sstevel@tonic-gate 	adt_event_data_t	*event	= NULL;
74*7c478bd9Sstevel@tonic-gate 	const char		*how = "couldn't start adt session";
75*7c478bd9Sstevel@tonic-gate 	int			saved_errno = 0;
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	if (adt_start_session(&ah, NULL, 0) != 0) {
78*7c478bd9Sstevel@tonic-gate 		saved_errno = errno;
79*7c478bd9Sstevel@tonic-gate 		goto fail;
80*7c478bd9Sstevel@tonic-gate 	}
81*7c478bd9Sstevel@tonic-gate 	if (adt_set_user(ah, uid, gid, uid, gid, NULL, ADT_NEW) != 0) {
82*7c478bd9Sstevel@tonic-gate 		saved_errno = errno;
83*7c478bd9Sstevel@tonic-gate 		how = "couldn't set adt user";
84*7c478bd9Sstevel@tonic-gate 		goto fail;
85*7c478bd9Sstevel@tonic-gate 	}
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 	if ((event = adt_alloc_event(ah, ADT_passwd)) == NULL) {
88*7c478bd9Sstevel@tonic-gate 		saved_errno = errno;
89*7c478bd9Sstevel@tonic-gate 		how = "couldn't allocate adt event";
90*7c478bd9Sstevel@tonic-gate 		goto fail;
91*7c478bd9Sstevel@tonic-gate 	}
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 	if (pam_retval == PAM_SUCCESS) {
94*7c478bd9Sstevel@tonic-gate 		if (adt_put_event(event, ADT_SUCCESS, ADT_SUCCESS) != 0) {
95*7c478bd9Sstevel@tonic-gate 			saved_errno = errno;
96*7c478bd9Sstevel@tonic-gate 			how = "couldn't put adt event";
97*7c478bd9Sstevel@tonic-gate 			goto fail;
98*7c478bd9Sstevel@tonic-gate 		}
99*7c478bd9Sstevel@tonic-gate 	} else if (adt_put_event(event, ADT_FAILURE,
100*7c478bd9Sstevel@tonic-gate 	    ADT_FAIL_PAM + pam_retval) != 0) {
101*7c478bd9Sstevel@tonic-gate 		saved_errno = errno;
102*7c478bd9Sstevel@tonic-gate 		how = "couldn't put adt event";
103*7c478bd9Sstevel@tonic-gate 		goto fail;
104*7c478bd9Sstevel@tonic-gate 	}
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 	adt_free_event(event);
107*7c478bd9Sstevel@tonic-gate 	(void) adt_end_session(ah);
108*7c478bd9Sstevel@tonic-gate 	return;
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate fail:
111*7c478bd9Sstevel@tonic-gate 	adt_free_event(event);
112*7c478bd9Sstevel@tonic-gate 	(void) adt_end_session(ah);
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	fatal("Auditing of password change failed: %s (%s)",
115*7c478bd9Sstevel@tonic-gate 	    strerror(saved_errno), how);
116*7c478bd9Sstevel@tonic-gate }
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate void
119*7c478bd9Sstevel@tonic-gate audit_sshd_login(adt_session_data_t **ah, uid_t uid, gid_t gid)
120*7c478bd9Sstevel@tonic-gate {
121*7c478bd9Sstevel@tonic-gate 	adt_event_data_t	*event	= NULL;
122*7c478bd9Sstevel@tonic-gate 	const char		*how = "couldn't start adt session";
123*7c478bd9Sstevel@tonic-gate 	int			saved_errno = 0;
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 	if (ah == NULL) {
126*7c478bd9Sstevel@tonic-gate 		how = "programmer error";
127*7c478bd9Sstevel@tonic-gate 		saved_errno = EINVAL;
128*7c478bd9Sstevel@tonic-gate 		goto fail;
129*7c478bd9Sstevel@tonic-gate 	}
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	if (adt_start_session(ah, NULL, ADT_USE_PROC_DATA) != 0) {
132*7c478bd9Sstevel@tonic-gate 		saved_errno = errno;
133*7c478bd9Sstevel@tonic-gate 		how = "couldn't start adt session";
134*7c478bd9Sstevel@tonic-gate 		goto fail;
135*7c478bd9Sstevel@tonic-gate 	}
136*7c478bd9Sstevel@tonic-gate 	if (adt_set_user(*ah, uid, gid, uid, gid,
137*7c478bd9Sstevel@tonic-gate 		    NULL, ADT_USER) != 0) {
138*7c478bd9Sstevel@tonic-gate 		saved_errno = errno;
139*7c478bd9Sstevel@tonic-gate 		how = "couldn't set adt user";
140*7c478bd9Sstevel@tonic-gate 		goto fail;
141*7c478bd9Sstevel@tonic-gate 	}
142*7c478bd9Sstevel@tonic-gate 	if ((event = adt_alloc_event(*ah, ADT_ssh)) == NULL) {
143*7c478bd9Sstevel@tonic-gate 		saved_errno = errno;
144*7c478bd9Sstevel@tonic-gate 		how = "couldn't allocate adt event";
145*7c478bd9Sstevel@tonic-gate 		goto fail;
146*7c478bd9Sstevel@tonic-gate 	}
147*7c478bd9Sstevel@tonic-gate 	if (adt_put_event(event, ADT_SUCCESS, ADT_SUCCESS) != 0) {
148*7c478bd9Sstevel@tonic-gate 		saved_errno = errno;
149*7c478bd9Sstevel@tonic-gate 		how = "couldn't put adt event";
150*7c478bd9Sstevel@tonic-gate 		goto fail;
151*7c478bd9Sstevel@tonic-gate 	}
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 	adt_free_event(event);
154*7c478bd9Sstevel@tonic-gate 	/* Don't end adt session - leave for when logging out */
155*7c478bd9Sstevel@tonic-gate 	return;
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate fail:
158*7c478bd9Sstevel@tonic-gate 	adt_free_event(event);
159*7c478bd9Sstevel@tonic-gate 	(void) adt_end_session(*ah);
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	fatal("Auditing of login failed: %s (%s)",
162*7c478bd9Sstevel@tonic-gate 	    strerror(saved_errno), how);
163*7c478bd9Sstevel@tonic-gate }
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate void
166*7c478bd9Sstevel@tonic-gate audit_sshd_login_failure(adt_session_data_t **ah, int pam_retval)
167*7c478bd9Sstevel@tonic-gate {
168*7c478bd9Sstevel@tonic-gate 	adt_event_data_t	*event	= NULL;
169*7c478bd9Sstevel@tonic-gate 	const char		*how = "couldn't start adt session";
170*7c478bd9Sstevel@tonic-gate 	int			saved_errno = 0;
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate 	if (ah == NULL) {
173*7c478bd9Sstevel@tonic-gate 		how = "programmer error";
174*7c478bd9Sstevel@tonic-gate 		saved_errno = EINVAL;
175*7c478bd9Sstevel@tonic-gate 		goto fail;
176*7c478bd9Sstevel@tonic-gate 	}
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 	if (adt_start_session(ah, NULL, ADT_USE_PROC_DATA) != 0) {
179*7c478bd9Sstevel@tonic-gate 		saved_errno = errno;
180*7c478bd9Sstevel@tonic-gate 		how = "couldn't start adt session";
181*7c478bd9Sstevel@tonic-gate 		goto fail;
182*7c478bd9Sstevel@tonic-gate 	}
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 	if (adt_set_user(*ah, ADT_NO_ATTRIB, ADT_NO_ATTRIB,
185*7c478bd9Sstevel@tonic-gate 	    ADT_NO_ATTRIB, ADT_NO_ATTRIB,
186*7c478bd9Sstevel@tonic-gate 	    NULL, ADT_NEW) != 0) {
187*7c478bd9Sstevel@tonic-gate 		saved_errno = errno;
188*7c478bd9Sstevel@tonic-gate 		how = "couldn't set adt user";
189*7c478bd9Sstevel@tonic-gate 		goto fail;
190*7c478bd9Sstevel@tonic-gate 	}
191*7c478bd9Sstevel@tonic-gate 	if ((event = adt_alloc_event(*ah, ADT_ssh)) == NULL) {
192*7c478bd9Sstevel@tonic-gate 		saved_errno = errno;
193*7c478bd9Sstevel@tonic-gate 		how = "couldn't allocate adt event";
194*7c478bd9Sstevel@tonic-gate 		goto fail;
195*7c478bd9Sstevel@tonic-gate 	}
196*7c478bd9Sstevel@tonic-gate 	if (adt_put_event(event, ADT_FAILURE, ADT_FAIL_PAM + pam_retval) != 0) {
197*7c478bd9Sstevel@tonic-gate 		saved_errno = errno;
198*7c478bd9Sstevel@tonic-gate 		how = "couldn't put adt event";
199*7c478bd9Sstevel@tonic-gate 		goto fail;
200*7c478bd9Sstevel@tonic-gate 	}
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate 	adt_free_event(event);
203*7c478bd9Sstevel@tonic-gate 	(void) adt_end_session(*ah);
204*7c478bd9Sstevel@tonic-gate 	*ah = NULL;
205*7c478bd9Sstevel@tonic-gate 	return;
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate fail:
208*7c478bd9Sstevel@tonic-gate 	adt_free_event(event);
209*7c478bd9Sstevel@tonic-gate 	(void) adt_end_session(*ah);
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 	fatal("Auditing of login failed: %s (%s)",
212*7c478bd9Sstevel@tonic-gate 	    strerror(saved_errno), how);
213*7c478bd9Sstevel@tonic-gate }
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate void
216*7c478bd9Sstevel@tonic-gate audit_sshd_logout(adt_session_data_t **ah)
217*7c478bd9Sstevel@tonic-gate {
218*7c478bd9Sstevel@tonic-gate 	adt_event_data_t	*event	= NULL;
219*7c478bd9Sstevel@tonic-gate 	const char		*how = "programmer error";
220*7c478bd9Sstevel@tonic-gate 	int			saved_errno = 0;
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate 	if (!ah) {
223*7c478bd9Sstevel@tonic-gate 		saved_errno = EINVAL;
224*7c478bd9Sstevel@tonic-gate 		goto fail;
225*7c478bd9Sstevel@tonic-gate 	}
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 	if ((event = adt_alloc_event(*ah, ADT_logout)) == NULL) {
228*7c478bd9Sstevel@tonic-gate 		saved_errno = errno;
229*7c478bd9Sstevel@tonic-gate 		how = "couldn't allocate adt event";
230*7c478bd9Sstevel@tonic-gate 		goto fail;
231*7c478bd9Sstevel@tonic-gate 	}
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate 	if (adt_put_event(event, ADT_SUCCESS, ADT_SUCCESS) != 0) {
234*7c478bd9Sstevel@tonic-gate 		saved_errno = errno;
235*7c478bd9Sstevel@tonic-gate 		how = "couldn't put adt event";
236*7c478bd9Sstevel@tonic-gate 		goto fail;
237*7c478bd9Sstevel@tonic-gate 	}
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate 	adt_free_event(event);
240*7c478bd9Sstevel@tonic-gate 	(void) adt_end_session(*ah);
241*7c478bd9Sstevel@tonic-gate 	*ah = NULL;
242*7c478bd9Sstevel@tonic-gate 	return;
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate fail:
245*7c478bd9Sstevel@tonic-gate 	adt_free_event(event);
246*7c478bd9Sstevel@tonic-gate 	(void) adt_end_session(*ah);
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 	fatal("Auditing of logout failed: %s (%s)",
249*7c478bd9Sstevel@tonic-gate 	    how, strerror(saved_errno));
250*7c478bd9Sstevel@tonic-gate }
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate /*
253*7c478bd9Sstevel@tonic-gate  * audit_sshd_settid stores the terminal id while it is still
254*7c478bd9Sstevel@tonic-gate  * available.
255*7c478bd9Sstevel@tonic-gate  *
256*7c478bd9Sstevel@tonic-gate  * The failure cases are lack of resources or incorrect permissions.
257*7c478bd9Sstevel@tonic-gate  * libbsm generates syslog messages, so there's no value doing more
258*7c478bd9Sstevel@tonic-gate  * here.  ADT_NO_AUDIT leaves the auid at AU_NOAUDITID and will be
259*7c478bd9Sstevel@tonic-gate  * replaced when one of the above functions is called.
260*7c478bd9Sstevel@tonic-gate  */
261*7c478bd9Sstevel@tonic-gate void
262*7c478bd9Sstevel@tonic-gate audit_sshd_settid(int sock)
263*7c478bd9Sstevel@tonic-gate {
264*7c478bd9Sstevel@tonic-gate 	adt_session_data_t	*ah;
265*7c478bd9Sstevel@tonic-gate 	adt_termid_t		*termid;
266*7c478bd9Sstevel@tonic-gate 
267*7c478bd9Sstevel@tonic-gate 	if (adt_start_session(&ah, NULL, 0) == 0) {
268*7c478bd9Sstevel@tonic-gate 		if (adt_load_termid(sock, &termid) == 0) {
269*7c478bd9Sstevel@tonic-gate 			if (adt_set_user(ah, ADT_NO_AUDIT,
270*7c478bd9Sstevel@tonic-gate 			    ADT_NO_AUDIT, 0, ADT_NO_AUDIT,
271*7c478bd9Sstevel@tonic-gate 			    termid, ADT_SETTID) == 0)
272*7c478bd9Sstevel@tonic-gate 				(void) adt_set_proc(ah);
273*7c478bd9Sstevel@tonic-gate 			free(termid);
274*7c478bd9Sstevel@tonic-gate 		}
275*7c478bd9Sstevel@tonic-gate 		(void) adt_end_session(ah);
276*7c478bd9Sstevel@tonic-gate 	}
277*7c478bd9Sstevel@tonic-gate }
278