]>
git.saurik.com Git - apple/security.git/blob - Security/libsecurity_utilities/lib/ccaudit.cpp
2 * Copyright (c) 2004,2011,2014 Apple Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
19 #include <strings.h> // bcopy()
21 #include <bsm/libbsm.h>
22 #include <security_utilities/utilities.h>
23 #include <security_utilities/debugging.h>
24 #include <security_utilities/logging.h>
25 #include <security_utilities/errors.h>
26 #include <security_utilities/ccaudit.h>
30 namespace CommonCriteria
33 TerminalId::TerminalId()
35 if (audit_set_terminal_id(this) != kAUNoErr
)
37 Syslog::warning("setting terminal ID info failed; using defaults");
43 AuditToken::AuditToken(const audit_token_t
&token
)
46 ::audit_token_to_au32(token
, &mAuditId
, &mEuid
, &mEgid
, &mRuid
, &mRgid
, &mPid
, &mSessionId
, &mTerminalId
);
56 UnixError::check(::getaudit_addr(this, sizeof(*this)));
59 void AuditInfo::get(au_asid_t session
)
62 if (session
!= this->ai_asid
) {
63 // need to use higher-privileged call to get info about a session that is not our own
64 this->ai_asid
= session
;
65 UnixError::check(::auditon(A_GETSINFO_ADDR
, this, sizeof(*this)));
69 void AuditInfo::getPid(pid_t pid
)
71 auditpinfo_addr_t pinfo
;
72 memset(&pinfo
, 0, sizeof(pinfo
));
74 UnixError::check(::auditon(A_GETPINFO_ADDR
, &pinfo
, sizeof(pinfo
)));
80 UnixError::check(::setaudit_addr(this, sizeof(*this)));
83 void AuditInfo::create(uint64_t flags
, uid_t auid
/* = AU_DEFAUDITID */)
87 ai_asid
= AU_ASSIGN_ASID
;
88 ai_termid
.at_type
= AU_IPv4
;
90 UnixError::check(::setaudit_addr(this, sizeof(*this)));
94 void AuditSession::registerSession(void)
98 auinfo
.ai_auid
= mAuditId
;
99 auinfo
.ai_asid
= mSessionId
;
100 bcopy(&mTerminalId
, &(auinfo
.ai_termid
), sizeof(auinfo
.ai_termid
));
101 bcopy(&mEventMask
.get(), &(auinfo
.ai_mask
), sizeof(auinfo
.ai_mask
));
103 if (setaudit(&auinfo
) != 0)
105 if (errno
== ENOTSUP
)
106 Syslog::notice("Attempted to initialize auditing, but this kernel does not support auditing");
108 Syslog::warning("Could not initialize auditing (%m); continuing");
112 void AuditRecord::submit(const short event_code
, const int returnCode
,
115 // If we're not auditing, do nothing
116 if (!(au_get_state() == AUC_AUDITING
))
119 secdebug("ccaudit", "Submitting authorization audit record");
123 // XXX/gh 3574731: Fix BSM SPI so the const_cast<>s aren't necessary
129 tok
= au_to_text(const_cast<char *>(msg
));
130 ret
= audit_write_success(event_code
, const_cast<token_t
*>(tok
),
131 mAuditToken
.auditId(), mAuditToken
.euid(),
132 mAuditToken
.egid(), mAuditToken
.ruid(),
133 mAuditToken
.rgid(), mAuditToken
.pid(),
134 mAuditToken
.sessionId(),
135 const_cast<au_tid_t
*>(&(mAuditToken
.terminalId())));
139 ret
= audit_write_failure(event_code
, const_cast<char *>(msg
),
140 returnCode
, mAuditToken
.auditId(),
141 mAuditToken
.euid(), mAuditToken
.egid(),
142 mAuditToken
.ruid(), mAuditToken
.rgid(),
143 mAuditToken
.pid(), mAuditToken
.sessionId(),
144 const_cast<au_tid_t
*>(&(mAuditToken
.terminalId())));
147 MacOSError::throwMe(ret
);
151 } // end namespace CommonCriteria
152 } // end namespace Security