xref: /titanic_52/usr/src/lib/libbsm/common/audit_rshd.c (revision 9acbbeaf2a1ffe5c14b244867d427714fab43c5c)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 #pragma ident	"%Z%%M%	%I%	%E% SMI"
26 
27 #include <sys/types.h>
28 #include <stdio.h>
29 #include <sys/fcntl.h>
30 #include <bsm/audit.h>
31 #include <bsm/audit_record.h>
32 #include <bsm/audit_uevents.h>
33 #include <bsm/libbsm.h>
34 #include <bsm/audit_private.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <syslog.h>
38 #include <netinet/in.h>
39 #include <tsol/label.h>
40 #include <locale.h>
41 #include <unistd.h>
42 #include <generic.h>
43 
44 static au_event_t	rshd_event;	/* audit event number */
45 static uint32_t		rshd_addr[4];	/* peer address */
46 
47 static void generate_record(char *, char *, char *, int, char *);
48 static void setup_session(char *);
49 static int selected(uid_t, char *, au_event_t, int);
50 
51 int
52 audit_rshd_setup()
53 {
54 	rshd_event = AUE_rshd;
55 	return (0);
56 }
57 
58 /* ARGSUSED */
59 int
60 audit_rshd_fail(msg, hostname, remuser, locuser, cmdbuf)
61 char	*msg;		/* message containing failure information */
62 char	*hostname;		/* hostname of machine requesting service */
63 char	*remuser;		/* username at machine requesting service */
64 char	*locuser;		/* username of local machine */
65 char	*cmdbuf;		/* command line to be executed locally */
66 {
67 	if (cannot_audit(0)) {
68 		return (0);
69 	}
70 	generate_record(remuser, locuser, cmdbuf, -1, msg);
71 	return (0);
72 }
73 
74 /* ARGSUSED */
75 int
76 audit_rshd_success(hostname, remuser, locuser, cmdbuf)
77 char	*hostname;		/* hostname of machine requesting service */
78 char	*remuser;		/* username at machine requesting service */
79 char	*locuser;		/* username at local machine */
80 char	*cmdbuf;		/* command line to be executed locally */
81 {
82 	if (cannot_audit(0)) {
83 		return (0);
84 	}
85 	generate_record(remuser, locuser, cmdbuf, 0, "");
86 	setup_session(locuser);
87 	return (0);
88 }
89 
90 
91 #include <pwd.h>
92 
93 static void
94 generate_record(char *remuser,	/* username at machine requesting service */
95 		char *locuser,	/* username of local machine */
96 		char *cmdbuf,	/* command line to be executed locally */
97 		int sf_flag,	/* success (0) or failure (-1) flag */
98 		char *msg)	/* message containing failure information */
99 {
100 	int	rd;		/* audit record descriptor */
101 	char	buf[256];	/* temporary buffer */
102 	char	*tbuf;		/* temporary buffer */
103 	int	tlen;
104 	const char *gtxt;
105 	uid_t	uid;
106 	gid_t	gid;
107 	pid_t	pid;
108 	struct passwd *pwd;
109 	struct auditinfo_addr info;
110 
111 	if (cannot_audit(0)) {
112 		return;
113 	}
114 
115 	pwd = getpwnam(locuser);
116 	if (pwd == NULL) {
117 		uid = -1;
118 		gid = -1;
119 	} else {
120 		uid = pwd->pw_uid;
121 		gid = pwd->pw_gid;
122 	}
123 
124 	if (!selected(uid, locuser, rshd_event, sf_flag))
125 		return;
126 
127 	pid = getpid();
128 
129 	/* see if terminal id already set */
130 	if (getaudit_addr(&info, sizeof (info)) < 0) {
131 		perror("getaudit");
132 	}
133 	rd = au_open();
134 
135 	(void) au_write(rd, au_to_subject_ex(uid, uid, gid, uid, gid, pid, pid,
136 		&info.ai_termid));
137 	if (is_system_labeled())
138 		(void) au_write(rd, au_to_mylabel());
139 
140 	gtxt = dgettext(bsm_dom, "cmd %s");
141 	tlen = strlen(gtxt) + strlen(cmdbuf) + 1;
142 	if ((tbuf = malloc(tlen)) == NULL) {
143 		(void) au_close(rd, 0, 0);
144 		return;
145 	}
146 	(void) snprintf(tbuf, tlen, gtxt, cmdbuf);
147 	(void) au_write(rd, au_to_text(tbuf));
148 	(void) free(tbuf);
149 
150 	if (strcmp(remuser, locuser) != 0) {
151 		(void) snprintf(buf, sizeof (buf), dgettext(bsm_dom,
152 			"remote user %s"), remuser);
153 		(void) au_write(rd, au_to_text(buf));
154 	}
155 
156 	if (sf_flag == -1) {
157 		(void) snprintf(buf, sizeof (buf), dgettext(bsm_dom,
158 			"local user %s"), locuser);
159 		(void) au_write(rd, au_to_text(buf));
160 		(void) au_write(rd, au_to_text(msg));
161 	}
162 
163 #ifdef _LP64
164 	(void) au_write(rd, au_to_return64(sf_flag, (int64_t)0));
165 #else
166 	(void) au_write(rd, au_to_return32(sf_flag, (int32_t)0));
167 #endif
168 
169 	if (au_close(rd, 1, rshd_event) < 0) {
170 		(void) au_close(rd, 0, 0);
171 	}
172 }
173 
174 static int
175 selected(uid_t uid, char *locuser, au_event_t event, int sf)
176 {
177 	int	rc, sorf;
178 	char	naflags[512];
179 	struct au_mask mask;
180 
181 	mask.am_success = mask.am_failure = 0;
182 	if (uid < 0) {
183 		rc = getacna(naflags, 256); /* get non-attrib flags */
184 		if (rc == 0)
185 			(void) getauditflagsbin(naflags, &mask);
186 	} else {
187 		rc = au_user_mask(locuser, &mask);
188 	}
189 
190 	if (sf == 0)
191 		sorf = AU_PRS_SUCCESS;
192 	else if (sf == -1)
193 		sorf = AU_PRS_FAILURE;
194 	else
195 		sorf = AU_PRS_BOTH;
196 	rc = au_preselect(event, &mask, sorf, AU_PRS_REREAD);
197 	return (rc);
198 }
199 
200 static void
201 setup_session(char *locuser)
202 {
203 	int	rc;
204 	struct auditinfo_addr info;
205 	au_mask_t		mask;
206 	uid_t			uid;
207 	struct passwd *pwd;
208 
209 	pwd = getpwnam(locuser);
210 	if (pwd == NULL)
211 		uid = -1;
212 	else
213 		uid = pwd->pw_uid;
214 
215 	/* see if terminal id already set */
216 	if (getaudit_addr(&info, sizeof (info)) < 0) {
217 		perror("getaudit");
218 	}
219 
220 	info.ai_auid = uid;
221 	info.ai_asid = getpid();
222 
223 	mask.am_success = 0;
224 	mask.am_failure = 0;
225 	(void) au_user_mask(locuser, &mask);
226 
227 	info.ai_mask.am_success = mask.am_success;
228 	info.ai_mask.am_failure = mask.am_failure;
229 
230 	rshd_addr[0] = info.ai_termid.at_addr[0];
231 	rshd_addr[1] = info.ai_termid.at_addr[1];
232 	rshd_addr[2] = info.ai_termid.at_addr[2];
233 	rshd_addr[3] = info.ai_termid.at_addr[3];
234 
235 	rc = setaudit_addr(&info, sizeof (info));
236 	if (rc < 0) {
237 		perror("setaudit");
238 	}
239 }
240