10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51676Sjpk * Common Development and Distribution License (the "License").
61676Sjpk * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*12918SJan.Friedel@Sun.COM * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate #include <sys/types.h>
264321Scasper #include <sys/param.h>
270Sstevel@tonic-gate #include <stdio.h>
280Sstevel@tonic-gate #include <unistd.h>
290Sstevel@tonic-gate #include <sys/fcntl.h>
300Sstevel@tonic-gate #include <bsm/audit.h>
310Sstevel@tonic-gate #include <bsm/audit_record.h>
320Sstevel@tonic-gate #include <bsm/audit_uevents.h>
330Sstevel@tonic-gate #include <bsm/libbsm.h>
340Sstevel@tonic-gate #include <bsm/audit_private.h>
350Sstevel@tonic-gate #include <stdlib.h>
360Sstevel@tonic-gate #include <string.h>
370Sstevel@tonic-gate #include <syslog.h>
380Sstevel@tonic-gate #include <pwd.h>
390Sstevel@tonic-gate #include <netinet/in.h>
401676Sjpk #include <tsol/label.h>
410Sstevel@tonic-gate #include <locale.h>
420Sstevel@tonic-gate #include "generic.h"
430Sstevel@tonic-gate
440Sstevel@tonic-gate #ifdef C2_DEBUG
4511706SJan.Friedel@Sun.COM #define dprintf(x) { (void) printf x; }
460Sstevel@tonic-gate #else
470Sstevel@tonic-gate #define dprintf(x)
480Sstevel@tonic-gate #endif
490Sstevel@tonic-gate
500Sstevel@tonic-gate #define UNKNOWN_CMD "???"
510Sstevel@tonic-gate
520Sstevel@tonic-gate static au_event_t event;
530Sstevel@tonic-gate static int audit_rexd_status = 0;
540Sstevel@tonic-gate
550Sstevel@tonic-gate static char *
build_cmd(char ** cmd)560Sstevel@tonic-gate build_cmd(char **cmd)
570Sstevel@tonic-gate {
580Sstevel@tonic-gate int i, l;
590Sstevel@tonic-gate char *r;
600Sstevel@tonic-gate
610Sstevel@tonic-gate if (cmd == NULL)
620Sstevel@tonic-gate return (NULL);
630Sstevel@tonic-gate /* count the total length of command line */
640Sstevel@tonic-gate for (i = 0, l = 0; cmd[i] != NULL; i++)
650Sstevel@tonic-gate l += strlen(cmd[i]) + 1;
660Sstevel@tonic-gate
670Sstevel@tonic-gate if (l == 0)
680Sstevel@tonic-gate return (NULL);
690Sstevel@tonic-gate r = malloc(l);
700Sstevel@tonic-gate if (r != NULL) {
710Sstevel@tonic-gate for (i = 0; cmd[i] != NULL; i++) {
720Sstevel@tonic-gate (void) strcat(r, cmd[i]);
730Sstevel@tonic-gate if (cmd[i + 1] != NULL)
740Sstevel@tonic-gate (void) strcat(r, " ");
750Sstevel@tonic-gate }
760Sstevel@tonic-gate }
770Sstevel@tonic-gate return (r);
780Sstevel@tonic-gate }
790Sstevel@tonic-gate
800Sstevel@tonic-gate static int
selected(uid,user,event,sf)810Sstevel@tonic-gate selected(uid, user, event, sf)
820Sstevel@tonic-gate uid_t uid;
830Sstevel@tonic-gate char *user;
840Sstevel@tonic-gate au_event_t event;
850Sstevel@tonic-gate int sf;
860Sstevel@tonic-gate {
87*12918SJan.Friedel@Sun.COM int sorf;
88*12918SJan.Friedel@Sun.COM struct au_mask mask;
890Sstevel@tonic-gate
900Sstevel@tonic-gate mask.am_success = mask.am_failure = 0;
914321Scasper if (uid > MAXEPHUID) {
92*12918SJan.Friedel@Sun.COM /* get non-attrib flags */
93*12918SJan.Friedel@Sun.COM (void) auditon(A_GETKMASK, (caddr_t)&mask, sizeof (mask));
940Sstevel@tonic-gate } else {
95*12918SJan.Friedel@Sun.COM (void) au_user_mask(user, &mask);
960Sstevel@tonic-gate }
970Sstevel@tonic-gate
98*12918SJan.Friedel@Sun.COM if (sf == 0) {
990Sstevel@tonic-gate sorf = AU_PRS_SUCCESS;
100*12918SJan.Friedel@Sun.COM } else if (sf == -1) {
1010Sstevel@tonic-gate sorf = AU_PRS_FAILURE;
102*12918SJan.Friedel@Sun.COM } else {
1030Sstevel@tonic-gate sorf = AU_PRS_BOTH;
104*12918SJan.Friedel@Sun.COM }
105*12918SJan.Friedel@Sun.COM
106*12918SJan.Friedel@Sun.COM return (au_preselect(event, &mask, sorf, AU_PRS_REREAD));
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate void
audit_rexd_setup()1100Sstevel@tonic-gate audit_rexd_setup()
1110Sstevel@tonic-gate {
1120Sstevel@tonic-gate dprintf(("audit_rexd_setup()\n"));
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate event = AUE_rexd;
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate /* ARGSUSED */
1180Sstevel@tonic-gate static void
audit_rexd_session_setup(char * name,char * mach,uid_t uid)1190Sstevel@tonic-gate audit_rexd_session_setup(char *name, char *mach, uid_t uid)
1200Sstevel@tonic-gate {
1210Sstevel@tonic-gate int rc;
1220Sstevel@tonic-gate au_mask_t mask;
1230Sstevel@tonic-gate struct auditinfo_addr info;
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate if (getaudit_addr(&info, sizeof (info)) < 0) {
1260Sstevel@tonic-gate perror("getaudit_addr");
1270Sstevel@tonic-gate exit(1);
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate info.ai_auid = uid;
1310Sstevel@tonic-gate info.ai_asid = getpid();
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate mask.am_success = 0;
1340Sstevel@tonic-gate mask.am_failure = 0;
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate (void) au_user_mask(name, &mask);
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate info.ai_mask.am_success = mask.am_success;
1390Sstevel@tonic-gate info.ai_mask.am_failure = mask.am_failure;
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate rc = setaudit_addr(&info, sizeof (info));
1420Sstevel@tonic-gate if (rc < 0) {
1430Sstevel@tonic-gate perror("setaudit_addr");
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate void
audit_rexd_fail(msg,hostname,user,uid,gid,shell,cmd)1480Sstevel@tonic-gate audit_rexd_fail(msg, hostname, user, uid, gid, shell, cmd)
1490Sstevel@tonic-gate char *msg; /* message containing failure information */
1500Sstevel@tonic-gate char *hostname; /* hostname of machine requesting service */
1510Sstevel@tonic-gate char *user; /* username of user requesting service */
1520Sstevel@tonic-gate uid_t uid; /* user id of user requesting service */
1530Sstevel@tonic-gate gid_t gid; /* group of user requesting service */
1540Sstevel@tonic-gate char *shell; /* login shell of user requesting service */
1550Sstevel@tonic-gate char **cmd; /* argv to be executed locally */
1560Sstevel@tonic-gate {
1570Sstevel@tonic-gate int rd; /* audit record descriptor */
1580Sstevel@tonic-gate char buf[256]; /* temporary buffer */
1590Sstevel@tonic-gate char *tbuf; /* temporary buffer */
1600Sstevel@tonic-gate int tlen;
1610Sstevel@tonic-gate const char *gtxt; /* gettext return value */
1620Sstevel@tonic-gate pid_t pid;
1630Sstevel@tonic-gate char *cmdbuf;
1640Sstevel@tonic-gate char *audit_cmd[2] = {NULL, NULL};
1650Sstevel@tonic-gate int dont_free = 0;
1660Sstevel@tonic-gate struct auditinfo_addr info;
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate dprintf(("audit_rexd_fail()\n"));
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate /*
1710Sstevel@tonic-gate * check if audit_rexd_fail() or audit_rexd_success()
1720Sstevel@tonic-gate * have been called already.
1730Sstevel@tonic-gate */
1740Sstevel@tonic-gate if (audit_rexd_status == 1) {
1750Sstevel@tonic-gate return;
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate if (cannot_audit(0)) {
1790Sstevel@tonic-gate return;
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate /*
1830Sstevel@tonic-gate * set status to prevent multiple calls
1840Sstevel@tonic-gate * to audit_rexd_fail() and audit_rexd_success()
1850Sstevel@tonic-gate */
1860Sstevel@tonic-gate audit_rexd_status = 1;
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate /* determine if we're preselected */
1890Sstevel@tonic-gate if (!selected(uid, user, event, -1))
1900Sstevel@tonic-gate return;
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate pid = getpid();
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate if (getaudit_addr(&info, sizeof (info)) < 0) {
1950Sstevel@tonic-gate perror("getaudit_addr");
1960Sstevel@tonic-gate exit(1);
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate rd = au_open();
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate /* add subject token */
2020Sstevel@tonic-gate (void) au_write(rd,
2030Sstevel@tonic-gate au_to_subject_ex(uid, uid, gid, uid, gid, pid, pid,
2040Sstevel@tonic-gate &info.ai_termid));
2051676Sjpk if (is_system_labeled())
2061676Sjpk (void) au_write(rd, au_to_mylabel());
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate /* add reason for failure */
2090Sstevel@tonic-gate (void) au_write(rd, au_to_text(msg));
2100Sstevel@tonic-gate
2110Sstevel@tonic-gate /* add hostname of machine requesting service */
2120Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), dgettext(bsm_dom,
2130Sstevel@tonic-gate "Remote execution requested by: %s"), hostname);
2140Sstevel@tonic-gate (void) au_write(rd, au_to_text(buf));
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate /* add username of user requesting service */
2170Sstevel@tonic-gate if (user == NULL)
2180Sstevel@tonic-gate user = "???";
2190Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), dgettext(bsm_dom,
2200Sstevel@tonic-gate "Username: %s"), user);
2210Sstevel@tonic-gate (void) au_write(rd, au_to_text(buf));
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), dgettext(bsm_dom,
2240Sstevel@tonic-gate "User id: %d"), uid);
2250Sstevel@tonic-gate (void) au_write(rd, au_to_text(buf));
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate if (cmd == NULL) {
2280Sstevel@tonic-gate audit_cmd[0] = shell;
2290Sstevel@tonic-gate cmd = audit_cmd;
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate cmdbuf = build_cmd(cmd);
2330Sstevel@tonic-gate if (cmdbuf == NULL) {
2340Sstevel@tonic-gate cmdbuf = UNKNOWN_CMD;
2350Sstevel@tonic-gate dont_free = 1;
2360Sstevel@tonic-gate }
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate gtxt = dgettext(bsm_dom, "Command line: %s");
2390Sstevel@tonic-gate /* over estimate of size of buffer needed (%s is replaced) */
2400Sstevel@tonic-gate tlen = strlen(cmdbuf) + strlen(gtxt) + 1;
2410Sstevel@tonic-gate
2420Sstevel@tonic-gate if ((tbuf = malloc(tlen)) == NULL) {
2430Sstevel@tonic-gate (void) au_close(rd, 0, 0);
2440Sstevel@tonic-gate return;
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate (void) snprintf(tbuf, tlen, gtxt, cmdbuf);
2470Sstevel@tonic-gate (void) au_write(rd, au_to_text(tbuf));
2480Sstevel@tonic-gate (void) free(tbuf);
2490Sstevel@tonic-gate if (!dont_free)
2500Sstevel@tonic-gate (void) free(cmdbuf);
2510Sstevel@tonic-gate
2520Sstevel@tonic-gate /* add return token */
2530Sstevel@tonic-gate #ifdef _LP64
2540Sstevel@tonic-gate (void) au_write(rd, au_to_return64(-1, (int64_t)0));
2550Sstevel@tonic-gate #else
2560Sstevel@tonic-gate (void) au_write(rd, au_to_return32(-1, (int32_t)0));
2570Sstevel@tonic-gate #endif
2580Sstevel@tonic-gate
2590Sstevel@tonic-gate /* write audit record */
2600Sstevel@tonic-gate if (au_close(rd, 1, event) < 0) {
2610Sstevel@tonic-gate (void) au_close(rd, 0, 0);
2620Sstevel@tonic-gate return;
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate
2660Sstevel@tonic-gate void
audit_rexd_success(hostname,user,uid,gid,shell,cmd)2670Sstevel@tonic-gate audit_rexd_success(hostname, user, uid, gid, shell, cmd)
2680Sstevel@tonic-gate char *hostname; /* hostname of machine requesting service */
2690Sstevel@tonic-gate char *user; /* username of user requesting service, may be NULL */
2700Sstevel@tonic-gate uid_t uid; /* user id of user requesting service */
2710Sstevel@tonic-gate gid_t gid; /* group of user requesting service */
2720Sstevel@tonic-gate char *shell; /* login shell of user requesting service */
2730Sstevel@tonic-gate char **cmd; /* argv to be executed locally, may be NULL */
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate int rd; /* audit record descriptor */
2760Sstevel@tonic-gate char buf[256]; /* temporary buffer */
2770Sstevel@tonic-gate char *tbuf; /* temporary buffer */
2780Sstevel@tonic-gate int tlen;
2790Sstevel@tonic-gate const char *gtxt;
2800Sstevel@tonic-gate pid_t pid;
2810Sstevel@tonic-gate char *cmdbuf;
2820Sstevel@tonic-gate char *audit_cmd[2] = {NULL, NULL};
2830Sstevel@tonic-gate int dont_free = 0;
2840Sstevel@tonic-gate struct auditinfo_addr info;
2850Sstevel@tonic-gate char *empty = "";
2860Sstevel@tonic-gate
2870Sstevel@tonic-gate dprintf(("audit_rexd_success()\n"));
2880Sstevel@tonic-gate
2890Sstevel@tonic-gate /*
2900Sstevel@tonic-gate * check if audit_rexd_fail() or audit_rexd_success()
2910Sstevel@tonic-gate * have been called already.
2920Sstevel@tonic-gate */
2930Sstevel@tonic-gate if (audit_rexd_status == 1) {
2940Sstevel@tonic-gate return;
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate
2970Sstevel@tonic-gate if (cannot_audit(0)) {
2980Sstevel@tonic-gate return;
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate
3010Sstevel@tonic-gate /* a little bullet proofing... */
3020Sstevel@tonic-gate
3030Sstevel@tonic-gate if (hostname == NULL)
3040Sstevel@tonic-gate hostname = empty;
3050Sstevel@tonic-gate if (shell == NULL)
3060Sstevel@tonic-gate shell = empty;
3070Sstevel@tonic-gate
3080Sstevel@tonic-gate /*
3090Sstevel@tonic-gate * set status to prevent multiple calls
3100Sstevel@tonic-gate * to audit_rexd_fail() and audit_rexd_success()
3110Sstevel@tonic-gate */
3120Sstevel@tonic-gate audit_rexd_status = 1;
3130Sstevel@tonic-gate
3140Sstevel@tonic-gate /* determine if we're preselected */
3150Sstevel@tonic-gate if (!selected(uid, user, event, 0))
3160Sstevel@tonic-gate goto rexd_audit_session;
3170Sstevel@tonic-gate
3180Sstevel@tonic-gate pid = getpid();
3190Sstevel@tonic-gate
3200Sstevel@tonic-gate if (getaudit_addr(&info, sizeof (info)) < 0) {
3210Sstevel@tonic-gate perror("getaudit_addr");
3220Sstevel@tonic-gate exit(1);
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate
3250Sstevel@tonic-gate rd = au_open();
3260Sstevel@tonic-gate
3270Sstevel@tonic-gate /* add subject token */
3280Sstevel@tonic-gate (void) au_write(rd,
3290Sstevel@tonic-gate au_to_subject_ex(uid, uid, gid, uid, gid, pid, pid,
3300Sstevel@tonic-gate &info.ai_termid));
3311676Sjpk if (is_system_labeled())
3321676Sjpk (void) au_write(rd, au_to_mylabel());
3330Sstevel@tonic-gate
3340Sstevel@tonic-gate /* add hostname of machine requesting service */
3350Sstevel@tonic-gate
3360Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), dgettext(bsm_dom,
3370Sstevel@tonic-gate "Remote execution requested by: %s"), hostname);
3380Sstevel@tonic-gate (void) au_write(rd, au_to_text(buf));
3390Sstevel@tonic-gate
3400Sstevel@tonic-gate /* add username at machine requesting service */
3410Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), dgettext(bsm_dom,
3420Sstevel@tonic-gate "Username: %s"), user);
3430Sstevel@tonic-gate (void) au_write(rd, au_to_text(buf));
3440Sstevel@tonic-gate
3450Sstevel@tonic-gate if (cmd == NULL) {
3460Sstevel@tonic-gate audit_cmd[0] = shell;
3470Sstevel@tonic-gate cmd = audit_cmd;
3480Sstevel@tonic-gate }
3490Sstevel@tonic-gate
3500Sstevel@tonic-gate cmdbuf = build_cmd(cmd);
3510Sstevel@tonic-gate if (cmdbuf == NULL) {
3520Sstevel@tonic-gate cmdbuf = UNKNOWN_CMD;
3530Sstevel@tonic-gate dont_free = 1;
3540Sstevel@tonic-gate }
3550Sstevel@tonic-gate
3560Sstevel@tonic-gate gtxt = dgettext(bsm_dom, "Command line: %s");
3570Sstevel@tonic-gate tlen = strlen(cmdbuf) + strlen(gtxt) + 1;
3580Sstevel@tonic-gate
3590Sstevel@tonic-gate if ((tbuf = malloc(tlen)) == NULL) {
3600Sstevel@tonic-gate (void) au_close(rd, 0, 0);
3610Sstevel@tonic-gate goto rexd_audit_session;
3620Sstevel@tonic-gate }
3630Sstevel@tonic-gate
3640Sstevel@tonic-gate (void) snprintf(tbuf, tlen, gtxt, cmdbuf);
3650Sstevel@tonic-gate (void) au_write(rd, au_to_text(tbuf));
3660Sstevel@tonic-gate (void) free(tbuf);
3670Sstevel@tonic-gate if (!dont_free)
3680Sstevel@tonic-gate (void) free(cmdbuf);
3690Sstevel@tonic-gate
3700Sstevel@tonic-gate /* add return token */
3710Sstevel@tonic-gate #ifdef _LP64
3720Sstevel@tonic-gate (void) au_write(rd, au_to_return64(0, (int64_t)0));
3730Sstevel@tonic-gate #else
3740Sstevel@tonic-gate (void) au_write(rd, au_to_return32(0, (int32_t)0));
3750Sstevel@tonic-gate #endif
3760Sstevel@tonic-gate
3770Sstevel@tonic-gate /* write audit record */
3780Sstevel@tonic-gate if (au_close(rd, 1, event) < 0) {
3790Sstevel@tonic-gate (void) au_close(rd, 0, 0);
3800Sstevel@tonic-gate }
3810Sstevel@tonic-gate
3820Sstevel@tonic-gate rexd_audit_session:
3830Sstevel@tonic-gate audit_rexd_session_setup(user, hostname, uid);
3840Sstevel@tonic-gate }
385