/*
- * Copyright (c) 2000-2004 Apple Computer, Inc. All Rights Reserved.
+ * Copyright (c) 2000-2004,2008-2009 Apple Inc. All Rights Reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
#include <security_utilities/ccaudit.h> // AuditToken
+#include <sandbox.h>
+
using Authorization::AuthItemSet;
using Authorization::AuthItemRef;
using Authorization::AuthValue;
// Create an authorization token.
//
AuthorizationToken::AuthorizationToken(Session &ssn, const CredentialSet &base,
-const audit_token_t &auditToken)
+const audit_token_t &auditToken, bool operateAsLeastPrivileged)
: mBaseCreds(base), mTransferCount(INT_MAX),
- mCreatorCode(Server::process().clientCode()),
mCreatorPid(Server::process().pid()),
- mCreatorAuditToken(auditToken)
+ mCreatorAuditToken(auditToken),
+ mOperatesAsLeastPrivileged(operateAsLeastPrivileged)
{
mCreatorUid = mCreatorAuditToken.euid();
mCreatorGid = mCreatorAuditToken.egid();
+
+ if (sandbox_check(mCreatorPid, "authorization-right-obtain", SANDBOX_CHECK_NO_REPORT) != 0)
+ mCreatorSandboxed = true;
+ else
+ mCreatorSandboxed = false;
+
+ {
+ Process &thisProcess = Server::process();
+ StLock<Mutex> _(thisProcess);
+ if (SecCodeRef code = thisProcess.currentGuest())
+ MacOSError::check(SecCodeCopyStaticCode(code, kSecCSDefaultFlags, &mCreatorCode.aref()));
+ }
// link to session
referent(ssn);
authMap[mHandle] = this;
// all ready
- secdebug("SSauth", "Authorization %p created using %d credentials; owner=%s",
- this, int(mBaseCreds.size()),
- mCreatorCode ? mCreatorCode->encode().c_str() : "unknown");
+ secdebug("SSauth", "Authorization %p created using %d credentials; owner=%p",
+ this, int(mBaseCreds.size()), mCreatorCode.get());
}
AuthorizationToken::~AuthorizationToken()
}
+std::string AuthorizationToken::creatorPath() const
+{
+ if (mCreatorCode) {
+ StLock<Mutex> _(mLock);
+ CFRef<CFURLRef> path;
+ if (SecCodeCopyPath(mCreatorCode, kSecCSDefaultFlags, &path.aref()) == noErr)
+ return cfString(path);
+ }
+ return "unknown";
+}
+
+
//
// Locate an authorization given its blob.
//
}
void
-AuthorizationToken::setInfoSet(AuthItemSet &newInfoSet)
+AuthorizationToken::setInfoSet(AuthItemSet &newInfoSet, bool savePassword)
{
StLock<Mutex> _(mLock); // consider a separate lock
secdebug("SSauth", "Authorization %p setting new context", this);
+
+ AuthItemSet::const_iterator end = mInfoSet.end();
+ for (AuthItemSet::const_iterator it = mInfoSet.begin(); it != end; ++it) {
+ const AuthItemRef &item = *it;
+ if (0 == strcmp(item->name(), "password")) {
+ mSavedPassword.clear();
+ mSavedPassword.insert(item);
+ }
+ }
+
+ if (true == savePassword)
+ newInfoSet.insert(mSavedPassword.begin(), mSavedPassword.end());
+
mInfoSet = newInfoSet;
}
// This is destructive (non-merging)
void
-AuthorizationToken::setCredentialInfo(const Credential &inCred)
+AuthorizationToken::setCredentialInfo(const Credential &inCred, bool savePassword)
{
AuthItemSet dstInfoSet;
- char uid_string[16]; // fit a uid_t(u_int32_t)
-
- if (snprintf(uid_string, sizeof(uid_string), "%u", inCred->uid()) >=
- int(sizeof(uid_string)))
- uid_string[0] = '\0';
- AuthItemRef uidHint("uid", AuthValueOverlay(uid_string ? strlen(uid_string) + 1 : 0, uid_string), 0);
+
+ uid_t uid = inCred->uid();
+ AuthItemRef uidHint("uid", AuthValueOverlay(sizeof(uid), &uid));
dstInfoSet.insert(uidHint);
- AuthItemRef userHint("username", AuthValueOverlay(inCred->username()), 0);
+ AuthItemRef userHint("username", AuthValueOverlay(inCred->name()), 0);
dstInfoSet.insert(userHint);
- setInfoSet(dstInfoSet);
+ setInfoSet(dstInfoSet, savePassword);
}
void
{
AuthItemSet dstInfoSet;
secdebug("SSauth", "Authorization %p clearing context", this);
- setInfoSet(dstInfoSet);
+ setInfoSet(dstInfoSet, false);
}
void
-AuthorizationToken::scrubInfoSet()
+AuthorizationToken::scrubInfoSet(bool savePassword)
{
AuthItemSet srcInfoSet = infoSet(), dstInfoSet;
AuthItemSet::const_iterator end = srcInfoSet.end();
dstInfoSet.insert(item);
}
secdebug("SSauth", "Authorization %p scrubbing context", this);
- setInfoSet(dstInfoSet);
+ setInfoSet(dstInfoSet, savePassword);
}