2 * Copyright (c) 2000-2001 Apple Computer, 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.
20 // authority - authorization manager
22 #include "authority.h"
24 #include "connection.h"
28 #include "AuthorizationWalkers.h"
30 using Authorization::AuthItemSet
;
31 using Authorization::AuthItemRef
;
32 using Authorization::AuthValue
;
33 using Authorization::AuthValueOverlay
;
36 // The global dictionary of extant AuthorizationTokens
38 AuthorizationToken::AuthMap
AuthorizationToken::authMap
; // set of extant authorizations
39 Mutex
AuthorizationToken::authMapLock
; // lock for mAuthorizations (only)
43 // Construct an Authority
45 Authority::Authority(const char *configFile
)
46 : Authorization::Engine(configFile
)
50 Authority::~Authority()
56 // Create an authorization token.
58 AuthorizationToken::AuthorizationToken(Session
&ssn
, const CredentialSet
&base
, const audit_token_t
&auditToken
)
59 : session(ssn
), mBaseCreds(base
), mTransferCount(INT_MAX
),
60 mCreatorUid(auditToken
.val
[1]),
61 mCreatorGid(auditToken
.val
[2]),
62 mCreatorCode(Server::connection().process
.clientCode()),
63 mCreatorPid(Server::connection().process
.pid()),
64 mCreatorAuditToken(auditToken
)
66 // generate our (random) handle
67 Server::active().random(mHandle
);
69 // register handle in the global map
70 StLock
<Mutex
> _(authMapLock
);
71 authMap
[mHandle
] = this;
73 // register with parent session
74 session
.addAuthorization(this);
77 secdebug("SSauth", "Authorization %p created using %d credentials; owner=%s",
78 this, int(mBaseCreds
.size()),
79 mCreatorCode
? mCreatorCode
->encode().c_str() : "unknown");
82 AuthorizationToken::~AuthorizationToken()
85 assert(mUsingProcesses
.empty());
87 // deregister from parent session
88 if (session
.removeAuthorization(this))
91 secdebug("SSauth", "Authorization %p destroyed", this);
96 // Locate an authorization given its blob.
98 AuthorizationToken
&AuthorizationToken::find(const AuthorizationBlob
&blob
)
100 StLock
<Mutex
> _(authMapLock
);
101 AuthMap::iterator it
= authMap
.find(blob
);
102 if (it
== authMap
.end())
103 Authorization::Error::throwMe(errAuthorizationInvalidRef
);
109 // Handle atomic deletion of AuthorizationToken objects
111 AuthorizationToken::Deleter::Deleter(const AuthorizationBlob
&blob
)
114 AuthMap::iterator it
= authMap
.find(blob
);
115 if (it
== authMap
.end())
116 Authorization::Error::throwMe(errAuthorizationInvalidRef
);
120 void AuthorizationToken::Deleter::remove()
123 authMap
.erase(mAuth
->handle());
131 // Given a set of credentials, add it to our private credentials and return the result
133 // must hold Session::mCredsLock
134 CredentialSet
AuthorizationToken::effectiveCreds() const
136 secdebug("SSauth", "Authorization %p grabbing session %p creds %p",
137 this, &session
, &session
.authCredentials());
138 CredentialSet result
= session
.authCredentials();
139 for (CredentialSet::const_iterator it
= mBaseCreds
.begin(); it
!= mBaseCreds
.end(); it
++)
140 if (!(*it
)->isShared())
147 // Add more credential dependencies to an authorization
149 // must hold Session::mCredsLock
150 void AuthorizationToken::mergeCredentials(const CredentialSet
&add
)
152 secdebug("SSauth", "Authorization %p merge creds %p", this, &add
);
153 for (CredentialSet::const_iterator it
= add
.begin(); it
!= add
.end(); it
++) {
154 mBaseCreds
.erase(*it
);
155 mBaseCreds
.insert(*it
);
157 secdebug("SSauth", "Authorization %p merged %d new credentials for %d total",
158 this, int(add
.size()), int(mBaseCreds
.size()));
163 // Register a new process that uses this authorization token.
164 // This is an idempotent operation.
166 void AuthorizationToken::addProcess(Process
&proc
)
168 StLock
<Mutex
> _(mLock
);
169 mUsingProcesses
.insert(&proc
);
170 secdebug("SSauth", "Authorization %p added process %p(%d)", this, &proc
, proc
.pid());
175 // Completely unregister client process.
176 // It does not matter how often it was registered with addProcess before.
177 // This returns true if no more processes use this token. Presumably you
178 // would then want to clean up, though that's up to you.
180 bool AuthorizationToken::endProcess(Process
&proc
)
182 StLock
<Mutex
> _(mLock
);
183 assert(mUsingProcesses
.find(&proc
) != mUsingProcesses
.end());
184 mUsingProcesses
.erase(&proc
);
185 secdebug("SSauth", "Authorization %p removed process %p(%d)%s",
186 this, &proc
, proc
.pid(), mUsingProcesses
.empty() ? " FINAL" : "");
187 return mUsingProcesses
.empty();
192 // Check whether internalization/externalization is allowed
194 bool AuthorizationToken::mayExternalize(Process
&) const
196 return mTransferCount
> 0;
199 bool AuthorizationToken::mayInternalize(Process
&, bool countIt
)
201 StLock
<Mutex
> _(mLock
);
202 if (mTransferCount
> 0) {
205 secdebug("SSauth", "Authorization %p decrement intcount to %d", this, mTransferCount
);
213 AuthorizationToken::infoSet(AuthorizationString tag
)
215 StLock
<Mutex
> _(mLock
); // consider a separate lock
221 AuthItemSet::iterator found
= find_if(mInfoSet
.begin(), mInfoSet
.end(),
222 Authorization::FindAuthItemByRightName(tag
));
223 if (found
!= mInfoSet
.end())
224 tempSet
.insert(AuthItemRef(*found
));
230 secdebug("SSauth", "Authorization %p returning copy of context %s%s.", this, tag
? "for tag " : "", tag
? "" : tag
);
236 AuthorizationToken::setInfoSet(AuthItemSet
&newInfoSet
)
238 StLock
<Mutex
> _(mLock
); // consider a separate lock
239 secdebug("SSauth", "Authorization %p setting new context", this);
240 mInfoSet
= newInfoSet
;
243 // This is destructive (non-merging)
245 AuthorizationToken::setCredentialInfo(const Credential
&inCred
)
247 AuthItemSet dstInfoSet
;
248 char uid_string
[16]; // fit a uid_t(u_int32_t)
250 if (snprintf(uid_string
, sizeof(uid_string
), "%u", inCred
->uid()) >=
251 int(sizeof(uid_string
)))
252 uid_string
[0] = '\0';
253 AuthItemRef
uidHint("uid", AuthValueOverlay(uid_string
? strlen(uid_string
) + 1 : 0, uid_string
), 0);
254 dstInfoSet
.insert(uidHint
);
256 AuthItemRef
userHint("username", AuthValueOverlay(inCred
->username()), 0);
257 dstInfoSet
.insert(userHint
);
259 setInfoSet(dstInfoSet
);
263 AuthorizationToken::clearInfoSet()
265 AuthItemSet dstInfoSet
;
266 secdebug("SSauth", "Authorization %p clearing context", this);
267 setInfoSet(dstInfoSet
);