2 * Copyright (c) 2000-2006,2011-2012,2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <security_cdsa_utilities/AuthorizationData.h>
25 #include <security_cdsa_utilities/AuthorizationWalkers.h>
26 #include <security_cdsa_utilities/walkers.h>
27 #include <Security/checkpw.h>
32 // checkpw() that uses provided struct passwd
35 int checkpw_internal( const struct passwd
*pw
, const char* password
);
39 namespace Authorization
{
42 AuthValueRef::AuthValueRef(const AuthValue
&value
) :
43 RefPointer
<AuthValue
>(new AuthValue(value
)) {}
45 AuthValueRef::AuthValueRef(const AuthorizationValue
&value
) :
46 RefPointer
<AuthValue
>(new AuthValue(value
)) {}
48 AuthValue::AuthValue(const AuthorizationValue
&value
) :
51 mValue
.length
= value
.length
;
52 mValue
.data
= value
.data
;
55 AuthValueRef::AuthValueRef(UInt32 length
, void *data
) :
56 RefPointer
<AuthValue
>(new AuthValue(length
, data
)) {}
58 AuthValue::AuthValue(UInt32 length
, void *data
) :
61 mValue
.length
= length
;
62 mValue
.data
= new uint8_t[length
];
64 memcpy(mValue
.data
, data
, length
);
67 AuthValue::~AuthValue()
71 memset(mValue
.data
, 0, mValue
.length
);
72 delete[] reinterpret_cast<uint8_t*>(mValue
.data
);
77 AuthValue::operator = (const AuthValue
&other
)
81 memset(mValue
.data
, 0 , mValue
.length
);
82 delete[] reinterpret_cast<uint8_t*>(mValue
.data
);
85 mValue
= other
.mValue
;
86 mOwnsValue
= other
.mOwnsValue
;
87 other
.mOwnsValue
= false;
92 AuthValue::fillInAuthorizationValue(AuthorizationValue
&value
)
94 value
.length
= mValue
.length
;
95 value
.data
= mValue
.data
;
99 AuthValueVector::operator = (const AuthorizationValueVector
& valueVector
)
102 for (unsigned int i
=0; i
< valueVector
.count
; i
++)
103 push_back(AuthValueRef(valueVector
.values
[i
]));
108 AuthValueVector::copy(AuthorizationValueVector
**data
, size_t *length
) const
110 AuthorizationValueVector valueVector
;
111 valueVector
.count
= (UInt32
)size();
112 valueVector
.values
= new AuthorizationValue
[valueVector
.count
];
114 for (const_iterator it
= begin(); it
!= end(); ++it
, ++i
)
116 (*it
)->fillInAuthorizationValue(valueVector
.values
[i
]);
119 DataWalkers::Copier
<AuthorizationValueVector
> flatValueVector(&valueVector
);
120 *length
= flatValueVector
.length();
121 *data
= flatValueVector
.keep();
123 delete[] valueVector
.values
;
126 AuthItem::AuthItem(const AuthorizationItem
&item
) :
132 MacOSError::throwMe(errAuthorizationInternal
);
133 size_t nameLen
= strlen(item
.name
) + 1;
134 mName
= new char[nameLen
];
135 memcpy(const_cast<char *>(mName
), item
.name
, nameLen
);
137 mValue
.length
= item
.valueLength
;
138 mValue
.data
= new uint8_t[item
.valueLength
];
140 memcpy(mValue
.data
, item
.value
, item
.valueLength
);
144 AuthItem::AuthItem(AuthorizationString name
) :
154 AuthItem::AuthItem(AuthorizationString name
, AuthorizationValue value
, AuthorizationFlags flags
) :
160 MacOSError::throwMe(errAuthorizationInternal
);
161 size_t nameLen
= strlen(name
) + 1;
162 mName
= new char[nameLen
];
163 memcpy(const_cast<char *>(mName
), name
, nameLen
);
165 mValue
.length
= value
.length
;
166 mValue
.data
= new uint8_t[value
.length
];
168 memcpy(mValue
.data
, value
.data
, value
.length
);
171 AuthItem::~AuthItem()
177 memset(mValue
.data
, 0, mValue
.length
);
178 delete[] reinterpret_cast<uint8_t*>(mValue
.data
);
183 AuthItem::operator < (const AuthItem
&other
) const
185 return strcmp(mName
, other
.mName
) < 0;
189 AuthItem::operator = (const AuthItem
&other
)
195 memset(mValue
.data
, 0, mValue
.length
);
196 delete[] reinterpret_cast<uint8_t*>(mValue
.data
);
200 mValue
= other
.mValue
;
201 mFlags
= other
.mFlags
;
202 mOwnsName
= other
.mOwnsName
;
203 other
.mOwnsName
= false;
204 mOwnsValue
= other
.mOwnsValue
;
205 other
.mOwnsValue
= false;
210 AuthItem::fillInAuthorizationItem(AuthorizationItem
&item
)
213 item
.valueLength
= mValue
.length
;
214 item
.value
= mValue
.data
;
219 AuthItem::getBool(bool &value
)
221 if (mValue
.length
== sizeof(bool))
223 bool *tmpValue
= (bool *)mValue
.data
;
236 AuthItem::getString(string
&value
)
238 value
= string(static_cast<char*>(mValue
.data
), mValue
.length
);
243 AuthItem::getCssmData(CssmAutoData
&value
)
245 value
= CssmData(static_cast<uint8_t*>(mValue
.data
), mValue
.length
);
250 AuthItemRef::AuthItemRef(const AuthorizationItem
&item
) : RefPointer
<AuthItem
>(new AuthItem(item
)) {}
252 AuthItemRef::AuthItemRef(AuthorizationString name
) : RefPointer
<AuthItem
>(new AuthItem(name
)) {}
254 AuthItemRef::AuthItemRef(AuthorizationString name
, AuthorizationValue value
, AuthorizationFlags flags
) : RefPointer
<AuthItem
>(new AuthItem(name
, value
, flags
)) {}
260 AuthItemSet::AuthItemSet()
261 : firstItemName(NULL
)
265 AuthItemSet::~AuthItemSet()
267 if (NULL
!= firstItemName
)
272 AuthItemSet::operator = (const AuthorizationItemSet
& itemSet
)
276 for (unsigned int i
=0; i
< itemSet
.count
; i
++)
277 insert(AuthItemRef(itemSet
.items
[i
]));
283 AuthItemSet::operator=(const AuthItemSet
& itemSet
)
285 std::set
<AuthItemRef
>::operator=(itemSet
);
287 if (this != &itemSet
) {
294 AuthItemSet::AuthItemSet(const AuthorizationItemSet
*itemSet
)
295 : firstItemName(NULL
)
297 if (NULL
!= itemSet
&& NULL
!= itemSet
->items
)
299 if (0 < itemSet
->count
&& NULL
!= itemSet
->items
[0].name
)
300 firstItemName
= strdup(itemSet
->items
[0].name
);
302 for (unsigned int i
=0; i
< itemSet
->count
; i
++)
303 insert(AuthItemRef(itemSet
->items
[i
]));
307 AuthItemSet::AuthItemSet(const AuthItemSet
& itemSet
)
308 : std::set
<AuthItemRef
>(itemSet
)
314 AuthItemSet::duplicate(const AuthItemSet
& itemSet
)
316 if (itemSet
.firstItemName
!= NULL
)
317 firstItemName
= strdup(itemSet
.firstItemName
);
319 firstItemName
= NULL
;
323 AuthItemSet::copy(AuthorizationItemSet
*&data
, size_t &length
, Allocator
&alloc
) const
325 AuthorizationItemSet itemSet
;
326 itemSet
.count
= (UInt32
)size();
327 itemSet
.items
= new AuthorizationItem
[itemSet
.count
];
329 for (const_iterator it
= begin(); it
!= end(); ++it
, ++i
)
331 (*it
)->fillInAuthorizationItem(itemSet
.items
[i
]);
334 DataWalkers::Copier
<AuthorizationItemSet
> flatItemSet(&itemSet
, alloc
);
335 length
= flatItemSet
.length();
337 data
= flatItemSet
.keep();
338 // else flatItemSet disappears again
340 delete[] itemSet
.items
;
343 AuthorizationItemSet
*
344 AuthItemSet::copy() const
346 AuthorizationItemSet
*aCopy
;
348 copy(aCopy
, aLength
);
353 AuthItemSet::find(const char *name
)
355 AuthItemSet::const_iterator found
= find_if(this->begin(), this->end(), FindAuthItemByRightName(name
) );
356 if (found
!= this->end())
362 } // end namespace Authorization