5 * Created by G H on 3/24/09.
6 * Copyright (c) 2009 Apple Inc. All Rights Reserved.
8 * Extensions to utility classes in Security::CommonCriteria
9 * (libsecurity_utilities). Not clear that these are useful enough to be
10 * added there, so for now, they're here.
15 #include <Security/Authorization.h>
16 #include <bsm/audit_kevents.h> // AUE_NULL
17 #include <bsm/libbsm.h>
20 // Regarding message formats in comments, below:
22 // <> denotes a string with the indicated information
23 // '' denotes a literal string
25 // Message info is in text tokens unless otherwise indicated.
31 namespace CommonCriteria
38 // Pure virtual class from which audit log writers should be derived.
39 // The assumption about logging is that a "success" case logs certain
40 // data about what succeeded, while a "failure" case logs that same data
41 // plus some indication as to why the failure occurred.
43 // Subclasses minimally need to provide a writeCommon() method. They may
44 // override logSuccess(); q.v.
46 // An AuditLogger is intended to live no longer than the audit trailer of a
49 // setClientInfo() must be called before logging, or at best, gibberish
53 // "write" methods only au_write()
54 // "log" methods open, write, and close the log
59 AuditLogger() : mAuditFd(-1), mEvent(AUE_NULL
), mClientInfoSet(false) { }
60 AuditLogger(const audit_token_t
*srcToken
, short auEvent
= AUE_NULL
);
61 AuditLogger(const AuditToken
&srcToken
, short auEvent
= AUE_NULL
);
62 virtual ~AuditLogger();
64 bool open(); // false if auditing disabled; throws on real errors
65 void close(bool writeLog
= true); // throws if writeLog true but au_close() failed
67 void setClientInfo(const audit_token_t
*srcToken
);
68 void setClientInfo(const AuditToken
&srcToken
);
69 void setEvent(short auEvent
) { mEvent
= auEvent
; }
70 short event() const { return mEvent
; }
72 // common log-writing activities
73 void writeToken(token_t
*token
, const char *name
);
75 void writeReturn(char status
, int reterr
);
76 virtual void writeCommon() = 0; // should not open or close log
78 // logSuccess() assumes that all the ancillary information you need is
79 // written by writeCommon(). If that's not true, you can either
80 // override logSuccess() in your subclass, or use a different method
81 // altogether. Do not call AuditLogger::logSuccess() from the subclass
83 virtual void logSuccess();
85 virtual void logFailure(const char *errMsg
= NULL
, int errcode
= errAuthorizationDenied
);
86 virtual void logFailure(string
&errMsg
, int errcode
= errAuthorizationDenied
) { logFailure(errMsg
.c_str(), errcode
); }
88 // @@@ Extra credit: let callers add arbitrary tokens. Tokens added
89 // before a log*() call would be appended to the end of writeCommon()'s
93 void logInternalError(const char *fmt
, ...);
98 bool mClientInfoSet
; // disallow resetting client info
106 au_asid_t mAuditSessionId
;
107 au_tid_t mOldTerminalId
; // to cache audit_token_to_au32() result
108 au_tid_addr_t mTerminalId
; // @@@ AuditInfo still uses ai_tid_t
112 // KeychainAuthLogger format:
113 // 'System keychain authorization'
115 // <keychain item name>
116 // [optional] <more failure info>
118 // For QueryKeychainAuth audit logging
120 class KeychainAuthLogger
: public AuditLogger
122 static const char *sysKCAuthStr
;
123 static const char *unknownKCStr
;
124 static const char *unknownItemStr
;
127 KeychainAuthLogger() : AuditLogger(), mDatabase(unknownKCStr
), mItem(unknownItemStr
) { }
128 KeychainAuthLogger(const audit_token_t
*srcToken
, short auEvent
);
129 KeychainAuthLogger(const audit_token_t
*srcToken
, short auEvent
, const char *database
, const char *item
);
130 KeychainAuthLogger(const AuditToken
&srcToken
, short auEvent
);
131 KeychainAuthLogger(const AuditToken
&srcToken
, short auEvent
, const char *database
, const char *item
);
132 void setDbName(const char *database
);
133 void setItemName(const char *item
);
134 virtual void writeCommon();
142 // RightLogger provides basic common data and behavior for rights-based
143 // logging classes. @@@ "RightLogger" is a lousy name
148 static const char *unknownRightStr
;
151 RightLogger() : mRight(unknownRightStr
) { }
152 virtual ~RightLogger() { }
154 void setRight(const string
&rightName
);
155 void setRight(const char *rightName
);
162 // Basic (per-mechanism) AuthMechLogger format:
164 // [optional] 'mechanism' <mechanism name>
165 // [optional] <more info>
169 // mechanism FooPlugin:SomeMechanism
170 // unknown mechanism; ending rule evaluation
172 class AuthMechLogger
: public AuditLogger
, public RightLogger
174 static const char *unknownMechStr
;
175 static const char *mechStr
;
178 AuthMechLogger() : AuditLogger(), RightLogger(), mEvaluatingMechanism(false), mCurrentMechanism(unknownMechStr
) { }
179 AuthMechLogger(const AuditToken
&srcToken
, short auEvent
);
180 AuthMechLogger(const audit_token_t
*srcToken
, short auEvent
);
182 void setCurrentMechanism(const char *mech
); // pass NULL if not running mechs.
183 void setCurrentMechanism(const string
&mech
) { setCurrentMechanism(mech
.c_str()); }
184 virtual void writeCommon();
186 // Authorization mechanism-evaluation interrupts need to be logged since
187 // they cause evaluation to restart, possibly at a different point in the
189 void logInterrupt(const char *msg
); // NULL msg okay
190 void logInterrupt(string
&msg
) { logInterrupt(msg
.c_str()); }
193 bool mEvaluatingMechanism
;
194 string mCurrentMechanism
;
198 // Basic RightAuthenticationLogger formats:
200 // Per-credential (newly granted during an evaluation):
202 // UID of user performing the authentication [arg32 token]
203 // UID and username of the successfully authenticated user [arg32 token]
206 // UID of user performing the authentication [arg32 token]
207 // Name of the user as whom the first UID was attempting to authenticate
209 // Final (i.e., after all mechanisms) right-granting decision format:
211 // name of process requesting authorization
212 // name of process that created the Authorization handle
214 // Least-privilege credential-generating event format:
218 // @@@ each format should be its own class
220 class RightAuthenticationLogger
: public AuditLogger
, public RightLogger
222 static const char *unknownUserStr
;
223 static const char *unknownClientStr
;
224 static const char *unknownAuthCreatorStr
;
225 static const char *authenticatorStr
;
226 static const char *clientStr
;
227 static const char *authCreatorStr
;
228 static const char *authenticatedAsStr
;
229 static const char *leastPrivStr
;
232 RightAuthenticationLogger() : AuditLogger(), RightLogger() { }
233 RightAuthenticationLogger(const AuditToken
&srcToken
, short auEvent
);
234 RightAuthenticationLogger(const audit_token_t
*srcToken
, short auEvent
);
235 virtual ~RightAuthenticationLogger() { }
237 virtual void writeCommon();
239 virtual void logSuccess() { } // throw? in any case, don't allow the usual logSuccess() to work
240 // @@@ clean up, consolidate Success and AuthorizationResult
241 void logSuccess(uid_t authenticator
, uid_t target
, const char *targetName
);
242 void logAuthorizationResult(const char *client
, const char *authCreator
, int errcode
);
243 void logLeastPrivilege(uid_t userId
, bool isAuthorizingUser
);
244 virtual void logFailure(const char *errMsg
, int errcode
) { AuditLogger::logFailure(errMsg
, errcode
); }
245 void logAuthenticatorFailure(uid_t authenticator
, const char *targetName
);
249 } // namespace Securityd
251 } // namespace CommonCriteria
253 } // namespace Security