xref: /titanic_44/usr/src/lib/libbsm/common/audit_rshd.c (revision 749f21d359d8fbd020c974a1a5227316221bfc9c)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <stdio.h>
30 #include <sys/fcntl.h>
31 #include <bsm/audit.h>
32 #include <bsm/audit_record.h>
33 #include <bsm/audit_uevents.h>
34 #include <bsm/libbsm.h>
35 #include <bsm/audit_private.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <syslog.h>
39 #include <netinet/in.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 
138 	gtxt = dgettext(bsm_dom, "cmd %s");
139 	tlen = strlen(gtxt) + strlen(cmdbuf) + 1;
140 	if ((tbuf = malloc(tlen)) == NULL) {
141 		(void) au_close(rd, 0, 0);
142 		return;
143 	}
144 	(void) snprintf(tbuf, tlen, gtxt, cmdbuf);
145 	(void) au_write(rd, au_to_text(tbuf));
146 	(void) free(tbuf);
147 
148 	if (strcmp(remuser, locuser) != 0) {
149 		(void) snprintf(buf, sizeof (buf), dgettext(bsm_dom,
150 			"remote user %s"), remuser);
151 		(void) au_write(rd, au_to_text(buf));
152 	}
153 
154 	if (sf_flag == -1) {
155 		(void) snprintf(buf, sizeof (buf), dgettext(bsm_dom,
156 			"local user %s"), locuser);
157 		(void) au_write(rd, au_to_text(buf));
158 		(void) au_write(rd, au_to_text(msg));
159 	}
160 
161 #ifdef _LP64
162 	(void) au_write(rd, au_to_return64(sf_flag, (int64_t)0));
163 #else
164 	(void) au_write(rd, au_to_return32(sf_flag, (int32_t)0));
165 #endif
166 
167 	if (au_close(rd, 1, rshd_event) < 0) {
168 		(void) au_close(rd, 0, 0);
169 	}
170 }
171 
172 static int
173 selected(uid_t uid, char *locuser, au_event_t event, int sf)
174 {
175 	int	rc, sorf;
176 	char	naflags[512];
177 	struct au_mask mask;
178 
179 	mask.am_success = mask.am_failure = 0;
180 	if (uid < 0) {
181 		rc = getacna(naflags, 256); /* get non-attrib flags */
182 		if (rc == 0)
183 			(void) getauditflagsbin(naflags, &mask);
184 	} else {
185 		rc = au_user_mask(locuser, &mask);
186 	}
187 
188 	if (sf == 0)
189 		sorf = AU_PRS_SUCCESS;
190 	else if (sf == -1)
191 		sorf = AU_PRS_FAILURE;
192 	else
193 		sorf = AU_PRS_BOTH;
194 	rc = au_preselect(event, &mask, sorf, AU_PRS_REREAD);
195 	return (rc);
196 }
197 
198 static void
199 setup_session(char *locuser)
200 {
201 	int	rc;
202 	struct auditinfo_addr info;
203 	au_mask_t		mask;
204 	uid_t			uid;
205 	struct passwd *pwd;
206 
207 	pwd = getpwnam(locuser);
208 	if (pwd == NULL)
209 		uid = -1;
210 	else
211 		uid = pwd->pw_uid;
212 
213 	/* see if terminal id already set */
214 	if (getaudit_addr(&info, sizeof (info)) < 0) {
215 		perror("getaudit");
216 	}
217 
218 	info.ai_auid = uid;
219 	info.ai_asid = getpid();
220 
221 	mask.am_success = 0;
222 	mask.am_failure = 0;
223 	(void) au_user_mask(locuser, &mask);
224 
225 	info.ai_mask.am_success = mask.am_success;
226 	info.ai_mask.am_failure = mask.am_failure;
227 
228 	rshd_addr[0] = info.ai_termid.at_addr[0];
229 	rshd_addr[1] = info.ai_termid.at_addr[1];
230 	rshd_addr[2] = info.ai_termid.at_addr[2];
231 	rshd_addr[3] = info.ai_termid.at_addr[3];
232 
233 	rc = setaudit_addr(&info, sizeof (info));
234 	if (rc < 0) {
235 		perror("setaudit");
236 	}
237 }
238