2 * Copyright (c) 2000-2002 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 * Authorization.h -- APIs for implementing access control in applications
24 #ifndef _SECURITY_AUTHORIZATION_H_
25 #define _SECURITY_AUTHORIZATION_H_
27 #include <CoreServices/../Frameworks/CarbonCore.framework/Headers/MacTypes.h>
30 #if defined(__cplusplus)
37 Version 1.0 10/16/2000
39 The Authorization API contains all the APIs that a application or tool that need pre-authorization or need an authorization desision made.
41 A typical use cases are a preference panel that would start off calling AuthorizationCreate() (without UI) to get an authorization object. Then call AuthorizationCopyRights() to figure out what is currently allowed.
43 If any of the operations that the preference panel wishes to perform are currently not allowed the lock icon in the window would show up in the locked state. Otherwise it would show up unlocked.
45 When the user locks the lock AuthorizationFree() is called with the kAuthorizationFlagDestroyRights to destroy any authorization rights that have been aquired.
47 When the user unlocks the lock AuthorizationCreate() is called with the kAuthorizationFlagInteractionAllowed and kAuthorizationFlagExtendRights flags to obtain all required rights. The old authorization object can be freed by calling AuthorizationFree() with no flags.
54 @define kAuthorizationEmptyEnvironment
55 Parameter to specify to AuthorizationCreate when no environment is being provided.
57 #define kAuthorizationEmptyEnvironment NULL
61 @enum AuthorizationStatus
62 Error codes returned by Authorization API.
65 errAuthorizationSuccess
= 0, /* The operation completed successfully. */
66 errAuthorizationInvalidSet
= -60001, /* The set parameter is invalid. */
67 errAuthorizationInvalidRef
= -60002, /* The authorization parameter is invalid. */
68 errAuthorizationInvalidTag
= -60003, /* The tag parameter is invalid. */
69 errAuthorizationInvalidPointer
= -60004, /* The authorizedRights parameter is invalid. */
70 errAuthorizationDenied
= -60005, /* The authorization was denied. */
71 errAuthorizationCanceled
= -60006, /* The authorization was cancled by the user. */
72 errAuthorizationInteractionNotAllowed
= -60007, /* The authorization was denied since no user interaction was possible. */
73 errAuthorizationInternal
= -60008, /* something else went wrong */
74 errAuthorizationExternalizeNotAllowed
= -60009, /* authorization externalization denied */
75 errAuthorizationInternalizeNotAllowed
= -60010, /* authorization internalization denied */
76 errAuthorizationInvalidFlags
= -60011, /* invalid option flag(s) */
77 errAuthorizationToolExecuteFailure
= -60031, /* cannot execute privileged tool */
78 errAuthorizationToolEnvironmentError
= -60032 /* privileged tool environment error */
83 @enum AuthorizationFlags
84 Optional flags passed in to serveral Authorization APIs. See the description of AuthorizationCreate, AuthorizationCopyRights and AuthorizationFree for a description of how they affect those calls.
87 kAuthorizationFlagDefaults
= 0,
88 kAuthorizationFlagInteractionAllowed
= (1 << 0),
89 kAuthorizationFlagExtendRights
= (1 << 1),
90 kAuthorizationFlagPartialRights
= (1 << 2),
91 kAuthorizationFlagDestroyRights
= (1 << 3),
92 kAuthorizationFlagPreAuthorize
= (1 << 4),
94 // private bits (do not use)
95 kAuthorizationFlagNoData
= (1 << 20)
100 @typedef AuthorizationFlags
101 Optional flags passed in to AuthorizationCreate.
103 typedef UInt32 AuthorizationFlags
;
107 @enum AuthorizationRightFlags
108 Flags returned in the flags field of ItemSet Items when calling AuthorizationCopyRights().
111 kAuthorizationFlagCanNotPreAuthorize
= (1 << 0)
116 @typedef AuthorizationRef
117 Opaque reference to an authorization object.
119 typedef const struct AuthorizationOpaqueRef
*AuthorizationRef
;
123 @typedef AuthorizationString
124 A zero terminated string in UTF-8 encoding.
126 typedef const char *AuthorizationString
;
130 @typedef AuthorizationItem
131 Each AuthorizationItem describes a single string-named item with optional
132 parameter value. The value must be contiguous memory of valueLength bytes;
133 internal structure is defined separately for each name.
135 @param name name of the item, as an AuthorizationString. Mandatory.
136 @param value Pointer to the optional parameter value associated with name.
137 Must be NULL if no parameter value.
138 @param valueLength Number of bytes in parameter value. Must be zero if no parameter.
139 @param reserved Reserved field. Must be set to NULL on creation. Do not modify after that.
142 AuthorizationString name
;
150 @typedef AuthorizationItemSet
151 An AuthorizationItemSet structure represents a set of zero or more AuthorizationItems. Since it is a set it should not contain any identical AuthorizationItems.
153 @param count Number of items identified by items.
154 @param items Pointer to an array of items.
158 AuthorizationItem
*items
;
159 } AuthorizationItemSet
;
164 @typedef AuthorizationExternalForm
165 An AuthorizationExternalForm structure can hold the externalized form of
166 an AuthorizationRef. As such, it can be transmitted across IPC channels
167 to other processes, which can re-internalize it to recover a valid AuthorizationRef
169 The data contained in an AuthorizationExternalForm should be considered opaque.
171 SECURITY NOTE: Applications should take care to not disclose the AuthorizationExternalForm to
172 potential attackers since it would authorize rights to them.
175 kAuthorizationExternalFormLength
= 32
179 char bytes
[kAuthorizationExternalFormLength
];
180 } AuthorizationExternalForm
;
185 @typedef AuthorizationRights
186 An AuthorizationItemSet representing a set of rights each with an associated argument (value).
187 Each argument value is as defined for the specific right they belong to. Argument values may not contain pointers as the should be copyable to different address spaces.
189 typedef AuthorizationItemSet AuthorizationRights
;
193 @typedef AuthorizationEnvironment
194 An AuthorizationItemSet representing environmental information of potential use
195 to authorization decisions.
197 typedef AuthorizationItemSet AuthorizationEnvironment
;
201 @function AuthorizationCreate
202 Create a new autorization object which can be used in other authorization calls. When the authorization is no longer needed AuthorizationFree should be called.
204 When the kAuthorizationFlagInteractionAllowed flag is set, user interaction will happen when required. Failing to set this flag will result in this call failing with a errAuthorizationInteractionNotAllowed status when interaction is required.
206 Setting the kAuthorizationFlagExtendRights flag will extend the currently available rights. If this flag is set the returned AuthorizationRef will grant all the rights requested when errAuthorizationSuccess is returned. If this flag is not set the operation will almost certainly succeed, but no attempt will be made to make the requested rights availible.
207 Call AuthorizationCopyRights to figure out which of the requested rights are granted by the returned AuthorizationRef.
209 Setting the kAuthorizationFlagPartialRights flag will cause this call to succeed if only some of the requested rights are being granted by the returned AuthorizationRef. Unless this flag is set this API will fail if not all the requested rights could be obtained.
211 Setting the kAuthorizationFlagDestroyRights flag will prevent any rights obtained during this call from being preserved after returning from this API (This is most useful when the authorization parameter is NULL and the caller doesn't want to affect the session state in any way).
213 Setting the kAuthorizationFlagPreAuthorize flag will pre authorize the requested rights so that at a later time -- by calling AuthorizationMakeExternalForm() follow by AuthorizationCreateFromExternalForm() -- the obtained rights can be used in a different process. Rights that can't be preauthorized will be treated as if they were authorized for the sake of returning an error (in other words if all rights are either authorized or could not be preauthorized this call will still succeed).
214 The rights which could not be preauthorized are not currently authorized and may fail to authorize when a later call to AuthorizationCopyRights() is made, unless the kAuthorizationFlagExtendRights and kAuthorizationFlagInteractionAllowed flags are set. Even then they might still fail if the user does not supply the correct credentials.
215 The reason for passing in this flag is to provide correct audit trail information and to avoid unnecessary user interaction.
217 @param rights (input/optional) An AuthorizationItemSet containing rights for which authorization is being requested. If none are specified the resulting AuthorizationRef will authorize nothing at all.
218 @param environment (input/optional) An AuthorizationItemSet containing enviroment state used when making the autorization decision. See the AuthorizationEnvironment type for details.
219 @param flags (input) options specified by the AuthorizationFlags enum. set all unused bits to zero to allow for future expansion.
220 @param authorization (output optional) A pointer to an AuthorizationRef to be returned. When the returned AuthorizationRef is no longer needed AuthorizationFree should be called to prevent anyone from using the aquired rights. If NULL is specified no new rights are returned, but the system will attempt to authorize all the requested rights and return the appropriate status.
222 @result errAuthorizationSuccess 0 authorization or all requested rights succeeded.
224 errAuthorizationDenied -60005 The authorization for one or more of the requested rights was denied.
226 errAuthorizationCanceled -60006 The authorization was cancled by the user.
228 errAuthorizationInteractionNotAllowed -60007 The authorization was denied since no interaction with the user was allowed.
230 OSStatus
AuthorizationCreate(const AuthorizationRights
*rights
,
231 const AuthorizationEnvironment
*environment
,
232 AuthorizationFlags flags
,
233 AuthorizationRef
*authorization
);
237 @function AuthorizationFree
238 Destroy an AutorizationRef object. If the kAuthorizationFlagDestroyRights flag is passed,
239 any rights associated with the authorization are lost. Otherwise, only local resources
240 are released, and the rights may still be available to other clients.
242 Setting the kAuthorizationFlagDestroyRights flag will prevent any rights that were obtained by the specified authorization object to be preserved after returning from this API. This effectivaly locks down all potentially shared authorizations.
244 @param authorization (input) The authorization object on which this operation is performed.
246 @param flags (input) Bit mask of option flags to this call.
248 @result errAuthorizationSuccess 0 No error.
250 errAuthorizationInvalidRef -60002 The authorization parameter is invalid.
252 OSStatus
AuthorizationFree(AuthorizationRef authorization
, AuthorizationFlags flags
);
256 @function AuthorizationCopyRights
257 Given a set of rights, return the subset that is currently authorized
258 by the AuthorizationRef given.
260 When the kAuthorizationFlagInteractionAllowed flag is set, user interaction will happen when required. Failing to set this flag will result in this call failing with a errAuthorizationInteractionNotAllowed status when interaction is required.
262 Setting the kAuthorizationFlagExtendRights flag will extend the currently available rights.
264 Setting the kAuthorizationFlagPartialRights flag will cause this call to succeed if only some of the requested rights are being granted by the returned AuthorizationRef. Unless this flag is set this API will fail if not all the requested rights could be obtained.
266 Setting the kAuthorizationFlagDestroyRights flag will prevent any additional rights obtained during this call from being preserved after returning from this API.
268 Setting the kAuthorizationFlagPreAuthorize flag will pre authorize the requested rights so that at a later time -- by calling AuthorizationMakeExternalForm() follow by AuthorizationCreateFromExternalForm() -- the obtained rights can be used in a different process. Rights that can't be preauthorized will be treated as if they were authorized for the sake of returning an error (in other words if all rights are either authorized or could not be preauthorized this call will still succeed), and they will be returned in authorizedRights with their kAuthorizationFlagCanNotPreAuthorize bit in the flags field set to 1.
269 The rights which could not be preauthorized are not currently authorized and may fail to authorize when a later call to AuthorizationCopyRights() is made, unless the kAuthorizationFlagExtendRights and kAuthorizationFlagInteractionAllowed flags are set. Even then they might still fail if the user does not supply the correct credentials.
270 The reason for passing in this flag is to provide correct audit trail information and to avoid unnecessary user interaction.
272 Setting the kAuthorizationFlagPreAuthorize flag will pre authorize the requested rights so that at a later time -- by calling AuthorizationMakeExternalForm() follow by AuthorizationCreateFromExternalForm() -- the obtained rights can be used in a different process. When this flags is specified rights that can't be preauthorized will be returned as if they were authorized with their kAuthorizationFlagCanNotPreAuthorize bit in the flags field set to 1. These rights are not currently authorized and may fail to authorize later unless kAuthorizationFlagExtendRights and kAuthorizationFlagInteractionAllowed flags are set when the actual authorization is done. And even then they might still fail if the user does not supply the correct credentials.
274 @param authorization (input) The authorization object on which this operation is performed.
275 @param rights (input) A rights set (see AuthorizationCreate).
276 @param environment (input/optional) An AuthorizationItemSet containing enviroment state used when making the autorization decision. See the AuthorizationEnvironment type for details.
277 @param flags (input) options specified by the AuthorizationFlags enum. set all unused bits to zero to allow for future expansion.
278 @param authorizedRights (output/optional) A pointer to a newly allocated AuthorizationInfoSet in which the authorized subset of rights are returned (authorizedRights should be deallocated by calling AuthorizationFreeInfoSet() when it is no longer needed). If NULL the only information returned is the status. Note that if the kAuthorizationFlagPreAuthorize flag was specified rights that could not be preauthorized are returned in authorizedRights, but their flags contains the kAuthorizationFlagCanNotPreAuthorize bit.
280 @result errAuthorizationSuccess 0 No error.
282 errAuthorizationInvalidRef -60002 The authorization parameter is invalid.
284 errAuthorizationInvalidSet -60001 The rights parameter is invalid.
286 errAuthorizationInvalidPointer -60004 The authorizedRights parameter is invalid.
288 OSStatus
AuthorizationCopyRights(AuthorizationRef authorization
,
289 const AuthorizationRights
*rights
,
290 const AuthorizationEnvironment
*environment
,
291 AuthorizationFlags flags
,
292 AuthorizationRights
**authorizedRights
);
296 @function AuthorizationCopyInfo
297 Returns sideband information (e.g. access credentials) obtained from a call to AuthorizationCreate. The format of this data depends of the tag specified.
299 @param authorization (input) The authorization object on which this operation is performed.
300 @param tag (input/optional) An optional string tag specifing which sideband information should be returned. When NULL is specified all available information is returned.
301 @param flags (input) options specified by the AuthorizationFlags enum. set all unused bits to zero to allow for future expansion.
302 @param info (output) A pointer to a newly allocated AuthorizationInfoSet in which the requested sideband infomation is returned (info should be deallocated by calling AuthorizationFreeInfoSet() when it is no longer needed).
304 @result errAuthorizationSuccess 0 No error.
306 errAuthorizationInvalidRef -60002 The authorization parameter is invalid.
308 errAuthorizationInvalidTag -60003 The tag parameter is invalid.
310 errAuthorizationInvalidPointer -60004 The info parameter is invalid.
312 OSStatus
AuthorizationCopyInfo(AuthorizationRef authorization
,
313 AuthorizationString tag
,
314 AuthorizationItemSet
**info
);
318 @function AuthorizationMakeExternalForm
319 Turn an Authorization into an external "byte blob" form so it can be
320 transmitted to another process.
321 Note that *storing* the external form somewhere will probably not do what
322 you want, since authorizations are bounded by sessions, processes, and possibly
323 time limits. This is for online transmission of authorizations.
325 @param authorization The (valid) authorization reference to externalize
326 @param extForm Pointer to an AuthorizationExternalForm variable to fill.
328 @result errAuthorizationSuccess 0 No error.
330 errAuthorizationExternalizeNotAllowed -60009 Externalizing this authorization is not allowed.
332 errAuthorizationInvalidRef -60002 The authorization parameter is invalid.
336 OSStatus
AuthorizationMakeExternalForm(AuthorizationRef authorization
,
337 AuthorizationExternalForm
*extForm
);
341 @function AuthorizationCreateFromExternalForm
342 Turn an Authorization into an external "byte blob" form so it can be
343 transmitted to another process.
344 Note that *storing* the external form somewhere will probably not do what
345 you want, since authorizations are bounded by sessions, processes, and possibly
346 time limits. This is for online transmission of authorizations.
348 @param extForm Pointer to an AuthorizationExternalForm value.
349 @param authorization Will be filled with a valid AuthorizationRef on success.
351 @result errAuthorizationInternalizeNotAllowed -60010 Internalizing this authorization is not allowed.
353 OSStatus
AuthorizationCreateFromExternalForm(const AuthorizationExternalForm
*extForm
,
354 AuthorizationRef
*authorization
);
358 @function AuthorizationFreeItemSet
359 Release the memory allocated for an AuthorizationItemSet that was allocated
362 @param set The AuthorizationItemSet to deallocate.
364 @result errAuthorizationSuccess 0 No error.
366 errAuthorizationInvalidSet -60001 The set parameter is invalid.
368 OSStatus
AuthorizationFreeItemSet(AuthorizationItemSet
*set
);
372 @function AuthorizationExecuteWithPrivileges
373 Run an executable tool with enhanced privileges after passing
374 suitable authorization procedures.
376 @param authorization An authorization reference that is used to authorize
377 access to the enhanced privileges. It is also passed to the tool for
378 further access control.
379 @param pathToTool Full pathname to the tool that should be executed
380 with enhanced privileges.
381 @param options Option bits (reserved). Must be zero.
382 @param arguments An argv-style vector of strings to be passed to the tool.
383 @param communicationsPipe Assigned a UNIX stdio FILE pointer for
384 a bidirectional pipe to communicate with the tool. The tool will have
385 this pipe as its standard I/O channels (stdin/stdout). If NULL, do not
386 establish a communications pipe.
388 OSStatus
AuthorizationExecuteWithPrivileges(AuthorizationRef authorization
,
389 const char *pathToTool
,
390 AuthorizationFlags options
,
391 char * const *arguments
,
392 FILE **communicationsPipe
);
396 @function AuthorizationCopyPrivilegedReference
397 From within a tool launched via the AuthorizationExecuteWithPrivileges function
398 ONLY, retrieve the AuthorizationRef originally passed to that function.
399 While AuthorizationExecuteWithPrivileges already verified the authorization to
400 launch your tool, the tool may want to avail itself of any additional pre-authorizations
401 the caller may have obtained through that reference.
403 OSStatus
AuthorizationCopyPrivilegedReference(AuthorizationRef
*authorization
,
404 AuthorizationFlags flags
);
407 #if defined(__cplusplus)
411 #endif /* !_SECURITY_AUTHORIZATION_H_ */