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 * AuthorizationData.cpp
23 * Created by Michael Brouwer on Thu Oct 12 2000.
24 * Copyright (c) 2000 Apple Computer Inc. All rights reserved.
28 #include "AuthorizationData.h"
32 #include <Security/checkpw.h>
37 // checkpw() that uses provided struct passwd
40 int checkpw_internal( const struct passwd
*pw
, const char* password
);
44 namespace Authorization
{
47 AuthValueRef::AuthValueRef(const AuthValue
&value
) :
48 RefPointer
<AuthValue
>(new AuthValue(value
)) {}
50 AuthValueRef::AuthValueRef(const AuthorizationValue
&value
) :
51 RefPointer
<AuthValue
>(new AuthValue(value
)) {}
53 AuthValue::AuthValue(const AuthorizationValue
&value
) :
56 mValue
.length
= value
.length
;
57 mValue
.data
= value
.data
;
60 AuthValueRef::AuthValueRef(UInt32 length
, void *data
) :
61 RefPointer
<AuthValue
>(new AuthValue(length
, data
)) {}
63 AuthValue::AuthValue(UInt32 length
, void *data
) :
66 mValue
.length
= length
;
67 mValue
.data
= new uint8_t[length
];
69 memcpy(mValue
.data
, data
, length
);
72 AuthValue::~AuthValue()
75 delete[] reinterpret_cast<uint8_t*>(mValue
.data
);
79 AuthValue::operator = (const AuthValue
&other
)
82 delete[] reinterpret_cast<uint8_t*>(mValue
.data
);
84 mValue
= other
.mValue
;
85 mOwnsValue
= other
.mOwnsValue
;
86 other
.mOwnsValue
= false;
91 AuthValue::fillInAuthorizationValue(AuthorizationValue
&value
)
93 value
.length
= mValue
.length
;
94 value
.data
= mValue
.data
;
98 AuthValueVector::operator = (const AuthorizationValueVector
& valueVector
)
101 for (unsigned int i
=0; i
< valueVector
.count
; i
++)
102 push_back(AuthValueRef(valueVector
.values
[i
]));
107 AuthValueVector::copy(AuthorizationValueVector
**data
, size_t *length
) const
109 AuthorizationValueVector valueVector
;
110 valueVector
.count
= size();
111 valueVector
.values
= new AuthorizationValue
[valueVector
.count
];
113 for (const_iterator it
= begin(); it
!= end(); ++it
, ++i
)
115 (*it
)->fillInAuthorizationValue(valueVector
.values
[i
]);
118 Copier
<AuthorizationValueVector
> flatValueVector(&valueVector
);
119 *length
= flatValueVector
.length();
120 *data
= flatValueVector
.keep();
122 delete[] valueVector
.values
;
125 AuthItem::AuthItem(const AuthorizationItem
&item
) :
131 MacOSError::throwMe(errAuthorizationInternal
);
132 size_t nameLen
= strlen(item
.name
) + 1;
133 mName
= new char[nameLen
];
134 memcpy(const_cast<char *>(mName
), item
.name
, nameLen
);
136 mValue
.length
= item
.valueLength
;
137 mValue
.data
= new uint8_t[item
.valueLength
];
139 memcpy(mValue
.data
, item
.value
, item
.valueLength
);
143 AuthItem::AuthItem(AuthorizationString name
) :
153 AuthItem::AuthItem(AuthorizationString name
, AuthorizationValue value
, AuthorizationFlags flags
) :
159 MacOSError::throwMe(errAuthorizationInternal
);
160 size_t nameLen
= strlen(name
) + 1;
161 mName
= new char[nameLen
];
162 memcpy(const_cast<char *>(mName
), name
, nameLen
);
164 mValue
.length
= value
.length
;
165 mValue
.data
= new uint8_t[value
.length
];
167 memcpy(mValue
.data
, value
.data
, value
.length
);
170 AuthItem::~AuthItem()
175 delete[] reinterpret_cast<uint8_t*>(mValue
.data
);
179 AuthItem::operator < (const AuthItem
&other
) const
181 return strcmp(mName
, other
.mName
) < 0;
185 AuthItem::operator = (const AuthItem
&other
)
190 delete[] reinterpret_cast<uint8_t*>(mValue
.data
);
193 mValue
= other
.mValue
;
194 mFlags
= other
.mFlags
;
195 mOwnsName
= other
.mOwnsName
;
196 other
.mOwnsName
= false;
197 mOwnsValue
= other
.mOwnsValue
;
198 other
.mOwnsValue
= false;
203 AuthItem::fillInAuthorizationItem(AuthorizationItem
&item
)
206 item
.valueLength
= mValue
.length
;
207 item
.value
= mValue
.data
;
212 AuthItemRef::AuthItemRef(const AuthorizationItem
&item
) : RefPointer
<AuthItem
>(new AuthItem(item
)) {}
214 AuthItemRef::AuthItemRef(AuthorizationString name
) : RefPointer
<AuthItem
>(new AuthItem(name
)) {}
216 AuthItemRef::AuthItemRef(AuthorizationString name
, AuthorizationValue value
, AuthorizationFlags flags
) : RefPointer
<AuthItem
>(new AuthItem(name
, value
, flags
)) {}
222 AuthItemSet::AuthItemSet()
226 AuthItemSet::~AuthItemSet()
231 AuthItemSet::operator = (const AuthorizationItemSet
& itemSet
)
235 for (unsigned int i
=0; i
< itemSet
.count
; i
++)
236 insert(AuthItemRef(itemSet
.items
[i
]));
241 AuthItemSet::AuthItemSet(const AuthorizationItemSet
*itemSet
)
245 for (unsigned int i
=0; i
< itemSet
->count
; i
++)
246 insert(AuthItemRef(itemSet
->items
[i
]));
251 AuthItemSet::copy(AuthorizationItemSet
*&data
, size_t &length
, CssmAllocator
&alloc
) const
253 AuthorizationItemSet itemSet
;
254 itemSet
.count
= size();
255 itemSet
.items
= new AuthorizationItem
[itemSet
.count
];
257 for (const_iterator it
= begin(); it
!= end(); ++it
, ++i
)
259 (*it
)->fillInAuthorizationItem(itemSet
.items
[i
]);
262 Copier
<AuthorizationItemSet
> flatItemSet(&itemSet
, alloc
);
263 length
= flatItemSet
.length();
265 data
= flatItemSet
.keep();
266 // else flatItemSet disappears again
268 delete[] itemSet
.items
;
272 // CredentialImpl class
275 // only for testing whether this credential is usable
276 CredentialImpl::CredentialImpl(const string
&username
, const uid_t uid
, const gid_t gid
, bool shared
) :
277 mUsername(username
), mShared(shared
), mUid(uid
), mGid(gid
), mCreationTime(CFAbsoluteTimeGetCurrent()), mValid(true)
281 // credential with validity based on username/password combination.
282 CredentialImpl::CredentialImpl(const string
&username
, const string
&password
, bool shared
) :
283 mUsername(username
), mShared(shared
), mCreationTime(CFAbsoluteTimeGetCurrent()), mValid(false)
285 // Calling into DirectoryServices can be a long term operation
286 Server::active().longTermActivity();
288 // try short name first
289 const char *user
= username
.c_str();
290 struct passwd
*pw
= getpwnam(user
);
296 secdebug("autheval", "user %s not found, creating invalid credential", user
);
300 mUsername
= string ( pw
->pw_name
);
304 const char *passwd
= password
.c_str();
305 int checkpw_status
= checkpw_internal(pw
, passwd
);
307 if (checkpw_status
!= CHECKPW_SUCCESS
)
309 secdebug("autheval", "checkpw() for user %s failed with error %d, creating invalid credential", user
, checkpw_status
);
313 secdebug("autheval", "checkpw() for user %s succeeded, creating%s credential",
314 user
, mShared
? " shared" : "");
324 CredentialImpl::~CredentialImpl()
329 CredentialImpl::operator < (const CredentialImpl
&other
) const
331 if (!mShared
&& other
.mShared
)
333 if (!other
.mShared
&& mShared
)
336 return mUsername
< other
.mUsername
;
339 // Returns true if this CredentialImpl should be shared.
341 CredentialImpl::isShared() const
348 CredentialImpl::merge(const CredentialImpl
&other
)
350 assert(mUsername
== other
.mUsername
);
352 if (other
.mValid
&& (!mValid
|| mCreationTime
< other
.mCreationTime
))
354 mCreationTime
= other
.mCreationTime
;
361 // The time at which this credential was obtained.
363 CredentialImpl::creationTime() const
365 return mCreationTime
;
368 // Return true iff this credential is valid.
370 CredentialImpl::isValid() const
376 CredentialImpl::invalidate()
384 Credential::Credential() :
385 RefPointer
<CredentialImpl
>(NULL
)
389 Credential::Credential(CredentialImpl
*impl
) :
390 RefPointer
<CredentialImpl
>(impl
)
394 Credential::Credential(const string
&username
, const uid_t uid
, const gid_t gid
, bool shared
) :
395 RefPointer
<CredentialImpl
>(new CredentialImpl(username
, uid
, gid
, shared
))
399 Credential::Credential(const string
&username
, const string
&password
, bool shared
) :
400 RefPointer
<CredentialImpl
>(new CredentialImpl(username
, password
, shared
))
404 Credential::~Credential()
409 Credential::operator < (const Credential
&other
) const
417 return (**this) < (*other
);
422 } // end namespace Authorization