1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 27*0Sstevel@tonic-gate 28*0Sstevel@tonic-gate #include <sys/types.h> 29*0Sstevel@tonic-gate #include <stdio.h> 30*0Sstevel@tonic-gate #include <unistd.h> 31*0Sstevel@tonic-gate #include <sys/fcntl.h> 32*0Sstevel@tonic-gate #include <bsm/audit.h> 33*0Sstevel@tonic-gate #include <bsm/audit_record.h> 34*0Sstevel@tonic-gate #include <bsm/audit_uevents.h> 35*0Sstevel@tonic-gate #include <bsm/libbsm.h> 36*0Sstevel@tonic-gate #include <bsm/audit_private.h> 37*0Sstevel@tonic-gate #include <stdlib.h> 38*0Sstevel@tonic-gate #include <string.h> 39*0Sstevel@tonic-gate #include <syslog.h> 40*0Sstevel@tonic-gate #include <pwd.h> 41*0Sstevel@tonic-gate #include <netinet/in.h> 42*0Sstevel@tonic-gate #include <locale.h> 43*0Sstevel@tonic-gate #include "generic.h" 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate #ifdef C2_DEBUG 46*0Sstevel@tonic-gate #define dprintf(x) { printf x; } 47*0Sstevel@tonic-gate #else 48*0Sstevel@tonic-gate #define dprintf(x) 49*0Sstevel@tonic-gate #endif 50*0Sstevel@tonic-gate 51*0Sstevel@tonic-gate static au_event_t event; 52*0Sstevel@tonic-gate static int audit_rexecd_status = 0; 53*0Sstevel@tonic-gate 54*0Sstevel@tonic-gate static int 55*0Sstevel@tonic-gate selected(uid, user, event, sf) 56*0Sstevel@tonic-gate uid_t uid; 57*0Sstevel@tonic-gate char *user; 58*0Sstevel@tonic-gate au_event_t event; 59*0Sstevel@tonic-gate int sf; 60*0Sstevel@tonic-gate { 61*0Sstevel@tonic-gate int rc, sorf; 62*0Sstevel@tonic-gate char naflags[512]; 63*0Sstevel@tonic-gate struct au_mask mask; 64*0Sstevel@tonic-gate 65*0Sstevel@tonic-gate mask.am_success = mask.am_failure = 0; 66*0Sstevel@tonic-gate if (uid < 0) { 67*0Sstevel@tonic-gate rc = getacna(naflags, 256); /* get non-attrib flags */ 68*0Sstevel@tonic-gate if (rc == 0) 69*0Sstevel@tonic-gate (void) getauditflagsbin(naflags, &mask); 70*0Sstevel@tonic-gate } else { 71*0Sstevel@tonic-gate rc = au_user_mask(user, &mask); 72*0Sstevel@tonic-gate } 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gate if (sf == 0) 75*0Sstevel@tonic-gate sorf = AU_PRS_SUCCESS; 76*0Sstevel@tonic-gate else if (sf == -1) 77*0Sstevel@tonic-gate sorf = AU_PRS_FAILURE; 78*0Sstevel@tonic-gate else 79*0Sstevel@tonic-gate sorf = AU_PRS_BOTH; 80*0Sstevel@tonic-gate rc = au_preselect(event, &mask, sorf, AU_PRS_REREAD); 81*0Sstevel@tonic-gate return (rc); 82*0Sstevel@tonic-gate } 83*0Sstevel@tonic-gate 84*0Sstevel@tonic-gate void 85*0Sstevel@tonic-gate audit_rexecd_setup() 86*0Sstevel@tonic-gate { 87*0Sstevel@tonic-gate dprintf(("audit_rexecd_setup()\n")); 88*0Sstevel@tonic-gate 89*0Sstevel@tonic-gate event = AUE_rexecd; 90*0Sstevel@tonic-gate } 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate static void 94*0Sstevel@tonic-gate audit_rexecd_session_setup(char *name, char *mach, uid_t uid) 95*0Sstevel@tonic-gate { 96*0Sstevel@tonic-gate int rc; 97*0Sstevel@tonic-gate au_mask_t mask; 98*0Sstevel@tonic-gate struct auditinfo_addr info; 99*0Sstevel@tonic-gate uint32_t addr[4], type; 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gate info.ai_auid = uid; 102*0Sstevel@tonic-gate info.ai_asid = getpid(); 103*0Sstevel@tonic-gate 104*0Sstevel@tonic-gate mask.am_success = 0; 105*0Sstevel@tonic-gate mask.am_failure = 0; 106*0Sstevel@tonic-gate 107*0Sstevel@tonic-gate (void) au_user_mask(name, &mask); 108*0Sstevel@tonic-gate 109*0Sstevel@tonic-gate info.ai_mask.am_success = mask.am_success; 110*0Sstevel@tonic-gate info.ai_mask.am_failure = mask.am_failure; 111*0Sstevel@tonic-gate 112*0Sstevel@tonic-gate rc = aug_get_machine(mach, addr, &type); 113*0Sstevel@tonic-gate if (rc < 0) { 114*0Sstevel@tonic-gate perror("get address"); 115*0Sstevel@tonic-gate } 116*0Sstevel@tonic-gate info.ai_termid.at_port = aug_get_port(); 117*0Sstevel@tonic-gate info.ai_termid.at_type = type; 118*0Sstevel@tonic-gate info.ai_termid.at_addr[0] = addr[0]; 119*0Sstevel@tonic-gate info.ai_termid.at_addr[1] = addr[1]; 120*0Sstevel@tonic-gate info.ai_termid.at_addr[2] = addr[2]; 121*0Sstevel@tonic-gate info.ai_termid.at_addr[3] = addr[3]; 122*0Sstevel@tonic-gate 123*0Sstevel@tonic-gate rc = setaudit_addr(&info, sizeof (info)); 124*0Sstevel@tonic-gate if (rc < 0) { 125*0Sstevel@tonic-gate perror("setaudit"); 126*0Sstevel@tonic-gate } 127*0Sstevel@tonic-gate } 128*0Sstevel@tonic-gate 129*0Sstevel@tonic-gate void 130*0Sstevel@tonic-gate audit_rexecd_fail(msg, hostname, user, cmdbuf) 131*0Sstevel@tonic-gate char *msg; /* message containing failure information */ 132*0Sstevel@tonic-gate char *hostname; /* hostname of machine requesting service */ 133*0Sstevel@tonic-gate char *user; /* username of user requesting service */ 134*0Sstevel@tonic-gate char *cmdbuf; /* command line to be executed locally */ 135*0Sstevel@tonic-gate { 136*0Sstevel@tonic-gate int rd; /* audit record descriptor */ 137*0Sstevel@tonic-gate char buf[256]; /* temporary buffer */ 138*0Sstevel@tonic-gate char *tbuf; /* temporary buffer */ 139*0Sstevel@tonic-gate int tlen; 140*0Sstevel@tonic-gate const char *gtxt; 141*0Sstevel@tonic-gate uid_t uid; 142*0Sstevel@tonic-gate gid_t gid; 143*0Sstevel@tonic-gate pid_t pid; 144*0Sstevel@tonic-gate au_tid_addr_t tid; 145*0Sstevel@tonic-gate struct passwd *pwd; 146*0Sstevel@tonic-gate uint32_t addr[4], type; 147*0Sstevel@tonic-gate int rc; 148*0Sstevel@tonic-gate 149*0Sstevel@tonic-gate dprintf(("audit_rexecd_fail()\n")); 150*0Sstevel@tonic-gate 151*0Sstevel@tonic-gate /* 152*0Sstevel@tonic-gate * check if audit_rexecd_fail() or audit_rexecd_success() 153*0Sstevel@tonic-gate * have been called already. 154*0Sstevel@tonic-gate */ 155*0Sstevel@tonic-gate if (audit_rexecd_status == 1) { 156*0Sstevel@tonic-gate return; 157*0Sstevel@tonic-gate } 158*0Sstevel@tonic-gate 159*0Sstevel@tonic-gate if (cannot_audit(0)) { 160*0Sstevel@tonic-gate return; 161*0Sstevel@tonic-gate } 162*0Sstevel@tonic-gate 163*0Sstevel@tonic-gate /* 164*0Sstevel@tonic-gate * set status to prevent multiple calls 165*0Sstevel@tonic-gate * to audit_rexecd_fail() and audit_rexecd_success() 166*0Sstevel@tonic-gate */ 167*0Sstevel@tonic-gate audit_rexecd_status = 1; 168*0Sstevel@tonic-gate 169*0Sstevel@tonic-gate pwd = getpwnam(user); 170*0Sstevel@tonic-gate if (pwd == NULL) { 171*0Sstevel@tonic-gate uid = -1; 172*0Sstevel@tonic-gate gid = -1; 173*0Sstevel@tonic-gate } else { 174*0Sstevel@tonic-gate uid = pwd->pw_uid; 175*0Sstevel@tonic-gate gid = pwd->pw_gid; 176*0Sstevel@tonic-gate } 177*0Sstevel@tonic-gate 178*0Sstevel@tonic-gate /* determine if we're preselected */ 179*0Sstevel@tonic-gate if (!selected(uid, user, event, -1)) 180*0Sstevel@tonic-gate return; 181*0Sstevel@tonic-gate 182*0Sstevel@tonic-gate pid = getpid(); 183*0Sstevel@tonic-gate rc = aug_get_machine(hostname, addr, &type); 184*0Sstevel@tonic-gate if (rc < 0) { 185*0Sstevel@tonic-gate perror("get address"); 186*0Sstevel@tonic-gate } 187*0Sstevel@tonic-gate 188*0Sstevel@tonic-gate tid.at_port = aug_get_port(); 189*0Sstevel@tonic-gate tid.at_addr[0] = addr[0]; 190*0Sstevel@tonic-gate tid.at_addr[1] = addr[1]; 191*0Sstevel@tonic-gate tid.at_addr[2] = addr[2]; 192*0Sstevel@tonic-gate tid.at_addr[3] = addr[3]; 193*0Sstevel@tonic-gate tid.at_type = type; 194*0Sstevel@tonic-gate 195*0Sstevel@tonic-gate rd = au_open(); 196*0Sstevel@tonic-gate 197*0Sstevel@tonic-gate /* add subject token */ 198*0Sstevel@tonic-gate (void) au_write(rd, 199*0Sstevel@tonic-gate au_to_subject_ex(uid, uid, gid, uid, gid, pid, pid, &tid)); 200*0Sstevel@tonic-gate 201*0Sstevel@tonic-gate /* add reason for failure */ 202*0Sstevel@tonic-gate (void) au_write(rd, au_to_text(msg)); 203*0Sstevel@tonic-gate 204*0Sstevel@tonic-gate /* add hostname of machine requesting service */ 205*0Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), dgettext(bsm_dom, 206*0Sstevel@tonic-gate "Remote execution requested by: %s"), hostname); 207*0Sstevel@tonic-gate (void) au_write(rd, au_to_text(buf)); 208*0Sstevel@tonic-gate 209*0Sstevel@tonic-gate /* add username of user requesting service */ 210*0Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), dgettext(bsm_dom, 211*0Sstevel@tonic-gate "Username: %s"), user); 212*0Sstevel@tonic-gate (void) au_write(rd, au_to_text(buf)); 213*0Sstevel@tonic-gate 214*0Sstevel@tonic-gate /* add command line to be executed locally */ 215*0Sstevel@tonic-gate gtxt = dgettext(bsm_dom, "Command line: %s"); 216*0Sstevel@tonic-gate tlen = strlen(gtxt) + strlen(cmdbuf) + 1; 217*0Sstevel@tonic-gate if ((tbuf = malloc(tlen)) == NULL) { 218*0Sstevel@tonic-gate (void) au_close(rd, 0, 0); 219*0Sstevel@tonic-gate return; 220*0Sstevel@tonic-gate } 221*0Sstevel@tonic-gate (void) snprintf(tbuf, tlen, gtxt, cmdbuf); 222*0Sstevel@tonic-gate (void) au_write(rd, au_to_text(tbuf)); 223*0Sstevel@tonic-gate (void) free(tbuf); 224*0Sstevel@tonic-gate 225*0Sstevel@tonic-gate /* add return token */ 226*0Sstevel@tonic-gate #ifdef _LP64 227*0Sstevel@tonic-gate (void) au_write(rd, au_to_return64(-1, (int64_t)0)); 228*0Sstevel@tonic-gate #else 229*0Sstevel@tonic-gate (void) au_write(rd, au_to_return32(-1, (int32_t)0)); 230*0Sstevel@tonic-gate #endif 231*0Sstevel@tonic-gate 232*0Sstevel@tonic-gate /* write audit record */ 233*0Sstevel@tonic-gate if (au_close(rd, 1, event) < 0) { 234*0Sstevel@tonic-gate (void) au_close(rd, 0, 0); 235*0Sstevel@tonic-gate return; 236*0Sstevel@tonic-gate } 237*0Sstevel@tonic-gate } 238*0Sstevel@tonic-gate 239*0Sstevel@tonic-gate void 240*0Sstevel@tonic-gate audit_rexecd_success(hostname, user, cmdbuf) 241*0Sstevel@tonic-gate char *hostname; /* hostname of machine requesting service */ 242*0Sstevel@tonic-gate char *user; /* username of user requesting service */ 243*0Sstevel@tonic-gate char *cmdbuf; /* command line to be executed locally */ 244*0Sstevel@tonic-gate { 245*0Sstevel@tonic-gate int rd; /* audit record descriptor */ 246*0Sstevel@tonic-gate char buf[256]; /* temporary buffer */ 247*0Sstevel@tonic-gate char *tbuf; /* temporary buffer */ 248*0Sstevel@tonic-gate int tlen; 249*0Sstevel@tonic-gate const char *gtxt; 250*0Sstevel@tonic-gate uid_t uid; 251*0Sstevel@tonic-gate gid_t gid; 252*0Sstevel@tonic-gate pid_t pid; 253*0Sstevel@tonic-gate au_tid_addr_t tid; 254*0Sstevel@tonic-gate struct passwd *pwd; 255*0Sstevel@tonic-gate uint32_t addr[4], type; 256*0Sstevel@tonic-gate int rc; 257*0Sstevel@tonic-gate 258*0Sstevel@tonic-gate dprintf(("audit_rexecd_success()\n")); 259*0Sstevel@tonic-gate 260*0Sstevel@tonic-gate /* 261*0Sstevel@tonic-gate * check if audit_rexecd_fail() or audit_rexecd_success() 262*0Sstevel@tonic-gate * have been called already. 263*0Sstevel@tonic-gate */ 264*0Sstevel@tonic-gate if (audit_rexecd_status == 1) { 265*0Sstevel@tonic-gate return; 266*0Sstevel@tonic-gate } 267*0Sstevel@tonic-gate 268*0Sstevel@tonic-gate if (cannot_audit(0)) { 269*0Sstevel@tonic-gate return; 270*0Sstevel@tonic-gate } 271*0Sstevel@tonic-gate 272*0Sstevel@tonic-gate /* 273*0Sstevel@tonic-gate * set status to prevent multiple calls 274*0Sstevel@tonic-gate * to audit_rexecd_fail() and audit_rexecd_success() 275*0Sstevel@tonic-gate */ 276*0Sstevel@tonic-gate audit_rexecd_status = 1; 277*0Sstevel@tonic-gate 278*0Sstevel@tonic-gate pwd = getpwnam(user); 279*0Sstevel@tonic-gate if (pwd == NULL) { 280*0Sstevel@tonic-gate uid = -1; 281*0Sstevel@tonic-gate gid = -1; 282*0Sstevel@tonic-gate } else { 283*0Sstevel@tonic-gate uid = pwd->pw_uid; 284*0Sstevel@tonic-gate gid = pwd->pw_gid; 285*0Sstevel@tonic-gate } 286*0Sstevel@tonic-gate 287*0Sstevel@tonic-gate /* determine if we're preselected */ 288*0Sstevel@tonic-gate if (!selected(uid, user, event, 0)) 289*0Sstevel@tonic-gate goto rexecd_audit_session; 290*0Sstevel@tonic-gate 291*0Sstevel@tonic-gate pid = getpid(); 292*0Sstevel@tonic-gate rc = aug_get_machine(hostname, addr, &type); 293*0Sstevel@tonic-gate if (rc < 0) { 294*0Sstevel@tonic-gate perror("get address"); 295*0Sstevel@tonic-gate } 296*0Sstevel@tonic-gate 297*0Sstevel@tonic-gate tid.at_port = aug_get_port(); 298*0Sstevel@tonic-gate tid.at_addr[0] = addr[0]; 299*0Sstevel@tonic-gate tid.at_addr[1] = addr[1]; 300*0Sstevel@tonic-gate tid.at_addr[2] = addr[2]; 301*0Sstevel@tonic-gate tid.at_addr[3] = addr[3]; 302*0Sstevel@tonic-gate tid.at_type = type; 303*0Sstevel@tonic-gate 304*0Sstevel@tonic-gate rd = au_open(); 305*0Sstevel@tonic-gate 306*0Sstevel@tonic-gate /* add subject token */ 307*0Sstevel@tonic-gate (void) au_write(rd, 308*0Sstevel@tonic-gate au_to_subject_ex(uid, uid, gid, uid, gid, pid, pid, &tid)); 309*0Sstevel@tonic-gate 310*0Sstevel@tonic-gate /* add hostname of machine requesting service */ 311*0Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), dgettext(bsm_dom, 312*0Sstevel@tonic-gate "Remote execution requested by: %s"), hostname); 313*0Sstevel@tonic-gate (void) au_write(rd, au_to_text(buf)); 314*0Sstevel@tonic-gate 315*0Sstevel@tonic-gate /* add username at machine requesting service */ 316*0Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), dgettext(bsm_dom, 317*0Sstevel@tonic-gate "Username: %s"), user); 318*0Sstevel@tonic-gate (void) au_write(rd, au_to_text(buf)); 319*0Sstevel@tonic-gate 320*0Sstevel@tonic-gate /* add command line to be executed locally */ 321*0Sstevel@tonic-gate gtxt = dgettext(bsm_dom, "Command line: %s"); 322*0Sstevel@tonic-gate tlen = strlen(gtxt) + strlen(cmdbuf) + 1; 323*0Sstevel@tonic-gate if ((tbuf = malloc(tlen)) == NULL) { 324*0Sstevel@tonic-gate (void) au_close(rd, 0, 0); 325*0Sstevel@tonic-gate } else { 326*0Sstevel@tonic-gate (void) snprintf(tbuf, tlen, gtxt, cmdbuf); 327*0Sstevel@tonic-gate (void) au_write(rd, au_to_text(tbuf)); 328*0Sstevel@tonic-gate (void) free(tbuf); 329*0Sstevel@tonic-gate 330*0Sstevel@tonic-gate /* add return token */ 331*0Sstevel@tonic-gate #ifdef _LP64 332*0Sstevel@tonic-gate (void) au_write(rd, au_to_return64(0, (int64_t)0)); 333*0Sstevel@tonic-gate #else 334*0Sstevel@tonic-gate (void) au_write(rd, au_to_return32(0, (int32_t)0)); 335*0Sstevel@tonic-gate #endif 336*0Sstevel@tonic-gate 337*0Sstevel@tonic-gate /* write audit record */ 338*0Sstevel@tonic-gate if (au_close(rd, 1, event) < 0) { 339*0Sstevel@tonic-gate (void) au_close(rd, 0, 0); 340*0Sstevel@tonic-gate } 341*0Sstevel@tonic-gate } 342*0Sstevel@tonic-gate 343*0Sstevel@tonic-gate rexecd_audit_session: 344*0Sstevel@tonic-gate audit_rexecd_session_setup(user, hostname, uid); 345*0Sstevel@tonic-gate } 346