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 (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
25 #include <sys/types.h>
26 #include <sys/param.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>
39 #include <netinet/in.h>
40 #include <tsol/label.h>
45 #define dprintf(x) { (void) printf x; }
50 #define UNKNOWN_CMD "???"
52 static au_event_t event
;
53 static int audit_rexd_status
= 0;
63 /* count the total length of command line */
64 for (i
= 0, l
= 0; cmd
[i
] != NULL
; i
++)
65 l
+= strlen(cmd
[i
]) + 1;
71 for (i
= 0; cmd
[i
] != NULL
; i
++) {
72 (void) strcat(r
, cmd
[i
]);
73 if (cmd
[i
+ 1] != NULL
)
74 (void) strcat(r
, " ");
81 selected(uid
, user
, event
, sf
)
90 mask
.am_success
= mask
.am_failure
= 0;
91 if (uid
> MAXEPHUID
) {
92 /* get non-attrib flags */
93 (void) auditon(A_GETKMASK
, (caddr_t
)&mask
, sizeof (mask
));
95 (void) au_user_mask(user
, &mask
);
99 sorf
= AU_PRS_SUCCESS
;
100 } else if (sf
== -1) {
101 sorf
= AU_PRS_FAILURE
;
106 return (au_preselect(event
, &mask
, sorf
, AU_PRS_REREAD
));
112 dprintf(("audit_rexd_setup()\n"));
119 audit_rexd_session_setup(char *name
, char *mach
, uid_t uid
)
123 struct auditinfo_addr info
;
125 if (getaudit_addr(&info
, sizeof (info
)) < 0) {
126 perror("getaudit_addr");
131 info
.ai_asid
= getpid();
136 (void) au_user_mask(name
, &mask
);
138 info
.ai_mask
.am_success
= mask
.am_success
;
139 info
.ai_mask
.am_failure
= mask
.am_failure
;
141 rc
= setaudit_addr(&info
, sizeof (info
));
143 perror("setaudit_addr");
148 audit_rexd_fail(msg
, hostname
, user
, uid
, gid
, shell
, cmd
)
149 char *msg
; /* message containing failure information */
150 char *hostname
; /* hostname of machine requesting service */
151 char *user
; /* username of user requesting service */
152 uid_t uid
; /* user id of user requesting service */
153 gid_t gid
; /* group of user requesting service */
154 char *shell
; /* login shell of user requesting service */
155 char **cmd
; /* argv to be executed locally */
157 int rd
; /* audit record descriptor */
158 char buf
[256]; /* temporary buffer */
159 char *tbuf
; /* temporary buffer */
161 const char *gtxt
; /* gettext return value */
164 char *audit_cmd
[2] = {NULL
, NULL
};
166 struct auditinfo_addr info
;
168 dprintf(("audit_rexd_fail()\n"));
171 * check if audit_rexd_fail() or audit_rexd_success()
172 * have been called already.
174 if (audit_rexd_status
== 1) {
178 if (cannot_audit(0)) {
183 * set status to prevent multiple calls
184 * to audit_rexd_fail() and audit_rexd_success()
186 audit_rexd_status
= 1;
188 /* determine if we're preselected */
189 if (!selected(uid
, user
, event
, -1))
194 if (getaudit_addr(&info
, sizeof (info
)) < 0) {
195 perror("getaudit_addr");
201 /* add subject token */
203 au_to_subject_ex(uid
, uid
, gid
, uid
, gid
, pid
, pid
,
205 if (is_system_labeled())
206 (void) au_write(rd
, au_to_mylabel());
208 /* add reason for failure */
209 (void) au_write(rd
, au_to_text(msg
));
211 /* add hostname of machine requesting service */
212 (void) snprintf(buf
, sizeof (buf
), dgettext(bsm_dom
,
213 "Remote execution requested by: %s"), hostname
);
214 (void) au_write(rd
, au_to_text(buf
));
216 /* add username of user requesting service */
219 (void) snprintf(buf
, sizeof (buf
), dgettext(bsm_dom
,
220 "Username: %s"), user
);
221 (void) au_write(rd
, au_to_text(buf
));
223 (void) snprintf(buf
, sizeof (buf
), dgettext(bsm_dom
,
224 "User id: %d"), uid
);
225 (void) au_write(rd
, au_to_text(buf
));
228 audit_cmd
[0] = shell
;
232 cmdbuf
= build_cmd(cmd
);
233 if (cmdbuf
== NULL
) {
234 cmdbuf
= UNKNOWN_CMD
;
238 gtxt
= dgettext(bsm_dom
, "Command line: %s");
239 /* over estimate of size of buffer needed (%s is replaced) */
240 tlen
= strlen(cmdbuf
) + strlen(gtxt
) + 1;
242 if ((tbuf
= malloc(tlen
)) == NULL
) {
243 (void) au_close(rd
, 0, 0);
246 (void) snprintf(tbuf
, tlen
, gtxt
, cmdbuf
);
247 (void) au_write(rd
, au_to_text(tbuf
));
252 /* add return token */
254 (void) au_write(rd
, au_to_return64(-1, (int64_t)0));
256 (void) au_write(rd
, au_to_return32(-1, (int32_t)0));
259 /* write audit record */
260 if (au_close(rd
, 1, event
) < 0) {
261 (void) au_close(rd
, 0, 0);
267 audit_rexd_success(hostname
, user
, uid
, gid
, shell
, cmd
)
268 char *hostname
; /* hostname of machine requesting service */
269 char *user
; /* username of user requesting service, may be NULL */
270 uid_t uid
; /* user id of user requesting service */
271 gid_t gid
; /* group of user requesting service */
272 char *shell
; /* login shell of user requesting service */
273 char **cmd
; /* argv to be executed locally, may be NULL */
275 int rd
; /* audit record descriptor */
276 char buf
[256]; /* temporary buffer */
277 char *tbuf
; /* temporary buffer */
282 char *audit_cmd
[2] = {NULL
, NULL
};
284 struct auditinfo_addr info
;
287 dprintf(("audit_rexd_success()\n"));
290 * check if audit_rexd_fail() or audit_rexd_success()
291 * have been called already.
293 if (audit_rexd_status
== 1) {
297 if (cannot_audit(0)) {
301 /* a little bullet proofing... */
303 if (hostname
== NULL
)
309 * set status to prevent multiple calls
310 * to audit_rexd_fail() and audit_rexd_success()
312 audit_rexd_status
= 1;
314 /* determine if we're preselected */
315 if (!selected(uid
, user
, event
, 0))
316 goto rexd_audit_session
;
320 if (getaudit_addr(&info
, sizeof (info
)) < 0) {
321 perror("getaudit_addr");
327 /* add subject token */
329 au_to_subject_ex(uid
, uid
, gid
, uid
, gid
, pid
, pid
,
331 if (is_system_labeled())
332 (void) au_write(rd
, au_to_mylabel());
334 /* add hostname of machine requesting service */
336 (void) snprintf(buf
, sizeof (buf
), dgettext(bsm_dom
,
337 "Remote execution requested by: %s"), hostname
);
338 (void) au_write(rd
, au_to_text(buf
));
340 /* add username at machine requesting service */
341 (void) snprintf(buf
, sizeof (buf
), dgettext(bsm_dom
,
342 "Username: %s"), user
);
343 (void) au_write(rd
, au_to_text(buf
));
346 audit_cmd
[0] = shell
;
350 cmdbuf
= build_cmd(cmd
);
351 if (cmdbuf
== NULL
) {
352 cmdbuf
= UNKNOWN_CMD
;
356 gtxt
= dgettext(bsm_dom
, "Command line: %s");
357 tlen
= strlen(cmdbuf
) + strlen(gtxt
) + 1;
359 if ((tbuf
= malloc(tlen
)) == NULL
) {
360 (void) au_close(rd
, 0, 0);
361 goto rexd_audit_session
;
364 (void) snprintf(tbuf
, tlen
, gtxt
, cmdbuf
);
365 (void) au_write(rd
, au_to_text(tbuf
));
370 /* add return token */
372 (void) au_write(rd
, au_to_return64(0, (int64_t)0));
374 (void) au_write(rd
, au_to_return32(0, (int32_t)0));
377 /* write audit record */
378 if (au_close(rd
, 1, event
) < 0) {
379 (void) au_close(rd
, 0, 0);
383 audit_rexd_session_setup(user
, hostname
, uid
);