1/* 2 * Copyright (c) 2000-2004,2007,2011-2012 Apple Inc. All Rights Reserved. 3 * 4 * @APPLE_LICENSE_HEADER_START@ 5 * 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 11 * file. 12 * 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. 20 * 21 * @APPLE_LICENSE_HEADER_END@ 22 */ 23 24 25/* 26 * Authorization.h -- APIs for implementing access control in applications 27 * and daemons. 28 */ 29 30#ifndef _SECURITY_AUTHORIZATION_H_ 31#define _SECURITY_AUTHORIZATION_H_ 32 33#include <TargetConditionals.h> 34#include <MacTypes.h> 35#include <Availability.h> 36 37#include <stdio.h> 38 39#if defined(__cplusplus) 40extern "C" { 41#endif 42 43 44/*! 45 @header Authorization 46 Version 1.0 10/16/2000 47 48 The Authorization API contains all the APIs that a application or tool that need pre-authorization or need an authorization desision made. 49 50 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. 51 52 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. 53 54 When the user locks the lock AuthorizationFree() is called with the kAuthorizationFlagDestroyRights to destroy any authorization rights that have been aquired. 55 56 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. 57 58*/ 59 60 61 62/*! 63 @defined kAuthorizationEmptyEnvironment 64 Parameter to specify to AuthorizationCreate when no environment is being provided. 65*/ 66#define kAuthorizationEmptyEnvironment NULL 67 68 69/*! 70 @enum AuthorizationStatus 71 Error codes returned by Authorization API. 72*/ 73 74/* 75 Note: the comments that appear after these errors are used to create SecErrorMessages.strings. 76 The comments must not be multi-line, and should be in a form meaningful to an end user. If 77 a different or additional comment is needed, it can be put in the header doc format, or on a 78 line that does not start with errZZZ. 79 80 errAuthorizationSuccess can't include a string as it's also errSecSuccess in libsecurity_keychain/lib/SecBase.h 81*/ 82 83enum { 84 errAuthorizationSuccess = 0, 85 errAuthorizationInvalidSet = -60001, /* The authorization rights are invalid. */ 86 errAuthorizationInvalidRef = -60002, /* The authorization reference is invalid. */ 87 errAuthorizationInvalidTag = -60003, /* The authorization tag is invalid. */ 88 errAuthorizationInvalidPointer = -60004, /* The returned authorization is invalid. */ 89 errAuthorizationDenied = -60005, /* The authorization was denied. */ 90 errAuthorizationCanceled = -60006, /* The authorization was cancelled by the user. */ 91 errAuthorizationInteractionNotAllowed = -60007, /* The authorization was denied since no user interaction was possible. */ 92 errAuthorizationInternal = -60008, /* Unable to obtain authorization for this operation. */ 93 errAuthorizationExternalizeNotAllowed = -60009, /* The authorization is not allowed to be converted to an external format. */ 94 errAuthorizationInternalizeNotAllowed = -60010, /* The authorization is not allowed to be created from an external format. */ 95 errAuthorizationInvalidFlags = -60011, /* The provided option flag(s) are invalid for this authorization operation. */ 96 errAuthorizationToolExecuteFailure = -60031, /* The specified program could not be executed. */ 97 errAuthorizationToolEnvironmentError = -60032, /* An invalid status was returned during execution of a privileged tool. */ 98 errAuthorizationBadAddress = -60033, /* The requested socket address is invalid (must be 0-1023 inclusive). */ 99}; 100 101 102/*! 103 @enum AuthorizationFlags 104 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. 105*/ 106enum { 107 kAuthorizationFlagDefaults = 0, 108 kAuthorizationFlagInteractionAllowed = (1 << 0), 109 kAuthorizationFlagExtendRights = (1 << 1), 110 kAuthorizationFlagPartialRights = (1 << 2), 111 kAuthorizationFlagDestroyRights = (1 << 3), 112 kAuthorizationFlagPreAuthorize = (1 << 4), 113 114 // private bits (do not use) 115 kAuthorizationFlagNoData = (1 << 20) 116}; 117 118 119/*! 120 @typedef AuthorizationFlags 121 Optional flags passed in to AuthorizationCreate. 122*/ 123typedef UInt32 AuthorizationFlags; 124 125 126/*! 127 @enum AuthorizationRightFlags 128 Flags returned in the flags field of ItemSet Items when calling AuthorizationCopyRights(). 129*/ 130enum { 131 kAuthorizationFlagCanNotPreAuthorize = (1 << 0) 132}; 133 134 135/*! 136 @typedef AuthorizationRef 137 Opaque reference to an authorization object. 138*/ 139typedef const struct AuthorizationOpaqueRef *AuthorizationRef; 140 141 142/*! 143 @typedef AuthorizationString 144 A zero terminated string in UTF-8 encoding. 145*/ 146typedef const char *AuthorizationString; 147 148 149/*! 150 @struct AuthorizationItem 151 Each AuthorizationItem describes a single string-named item with optional 152 parameter value. The value must be contiguous memory of valueLength bytes; 153 internal structure is defined separately for each name. 154 155 @field name name of the item, as an AuthorizationString. Mandatory. 156 @field valueLength Number of bytes in parameter value. Must be 0 if no parameter value. 157 @field value Pointer to the optional parameter value associated with name. 158 Must be NULL if no parameter value. 159 @field flags Reserved field. Must be set to 0 on creation. Do not modify after that. 160*/ 161typedef struct { 162 AuthorizationString name; 163 size_t valueLength; 164 void *value; 165 UInt32 flags; 166} AuthorizationItem; 167 168 169/*! 170 @struct AuthorizationItemSet 171 An AuthorizationItemSet structure represents a set of zero or more AuthorizationItems. Since it is a set it should not contain any identical AuthorizationItems. 172 173 @field count Number of items identified by items. 174 @field items Pointer to an array of items. 175*/ 176typedef struct { 177 UInt32 count; 178 AuthorizationItem *items; 179} AuthorizationItemSet; 180 181 182 183/*! 184 @struct AuthorizationExternalForm 185 An AuthorizationExternalForm structure can hold the externalized form of 186 an AuthorizationRef. As such, it can be transmitted across IPC channels 187 to other processes, which can re-internalize it to recover a valid AuthorizationRef 188 handle. 189 The data contained in an AuthorizationExternalForm should be considered opaque. 190 191 SECURITY NOTE: Applications should take care to not disclose the AuthorizationExternalForm to 192 potential attackers since it would authorize rights to them. 193*/ 194enum { 195 kAuthorizationExternalFormLength = 32 196}; 197 198typedef struct { 199 char bytes[kAuthorizationExternalFormLength]; 200} AuthorizationExternalForm; 201 202 203 204/*! 205 @typedef AuthorizationRights 206 An AuthorizationItemSet representing a set of rights each with an associated argument (value). 207 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. 208*/ 209typedef AuthorizationItemSet AuthorizationRights; 210 211 212/*! 213 @typedef AuthorizationEnvironment 214 An AuthorizationItemSet representing environmental information of potential use 215 to authorization decisions. 216*/ 217typedef AuthorizationItemSet AuthorizationEnvironment; 218 219 220/*! 221 @function AuthorizationCreate 222 Create a new autorization object which can be used in other authorization calls. When the authorization is no longer needed AuthorizationFree should be called. 223 224 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. 225 226 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. 227 Call AuthorizationCopyRights to figure out which of the requested rights are granted by the returned AuthorizationRef. 228 229 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. 230 231 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). 232 233 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). 234 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. 235 The reason for passing in this flag is to provide correct audit trail information and to avoid unnecessary user interaction. 236 237 @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. 238 @param environment (input/optional) An AuthorizationItemSet containing enviroment state used when making the autorization decision. See the AuthorizationEnvironment type for details. 239 @param flags (input) options specified by the AuthorizationFlags enum. set all unused bits to zero to allow for future expansion. 240 @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. 241 242 @result errAuthorizationSuccess 0 authorization or all requested rights succeeded. 243 244 errAuthorizationDenied -60005 The authorization for one or more of the requested rights was denied. 245 246 errAuthorizationCanceled -60006 The authorization was cancelled by the user. 247 248 errAuthorizationInteractionNotAllowed -60007 The authorization was denied since no interaction with the user was allowed. 249*/ 250OSStatus AuthorizationCreate(const AuthorizationRights *rights, 251 const AuthorizationEnvironment *environment, 252 AuthorizationFlags flags, 253 AuthorizationRef *authorization); 254 255 256/*! 257 @function AuthorizationFree 258 Destroy an AutorizationRef object. If the kAuthorizationFlagDestroyRights flag is passed, 259 any rights associated with the authorization are lost. Otherwise, only local resources 260 are released, and the rights may still be available to other clients. 261 262 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. 263 264 @param authorization (input) The authorization object on which this operation is performed. 265 266 @param flags (input) Bit mask of option flags to this call. 267 268 @result errAuthorizationSuccess 0 No error. 269 270 errAuthorizationInvalidRef -60002 The authorization parameter is invalid. 271*/ 272OSStatus AuthorizationFree(AuthorizationRef authorization, AuthorizationFlags flags); 273 274 275/*! 276 @function AuthorizationCopyRights 277 Given a set of rights, return the subset that is currently authorized 278 by the AuthorizationRef given. 279 280 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. 281 282 Setting the kAuthorizationFlagExtendRights flag will extend the currently available rights. 283 284 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. 285 286 Setting the kAuthorizationFlagDestroyRights flag will prevent any additional rights obtained during this call from being preserved after returning from this API. 287 288 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. 289 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. 290 The reason for passing in this flag is to provide correct audit trail information and to avoid unnecessary user interaction. 291 292 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. 293 294 @param authorization (input) The authorization object on which this operation is performed. 295 @param rights (input) A rights set (see AuthorizationCreate). 296 @param environment (input/optional) An AuthorizationItemSet containing enviroment state used when making the autorization decision. See the AuthorizationEnvironment type for details. 297 @param flags (input) options specified by the AuthorizationFlags enum. set all unused bits to zero to allow for future expansion. 298 @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 AuthorizationFreeItemSet() 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. 299 300 @result errAuthorizationSuccess 0 No error. 301 302 errAuthorizationInvalidRef -60002 The authorization parameter is invalid. 303 304 errAuthorizationInvalidSet -60001 The rights parameter is invalid. 305 306 errAuthorizationInvalidPointer -60004 The authorizedRights parameter is invalid. 307*/ 308OSStatus AuthorizationCopyRights(AuthorizationRef authorization, 309 const AuthorizationRights *rights, 310 const AuthorizationEnvironment *environment, 311 AuthorizationFlags flags, 312 AuthorizationRights **authorizedRights); 313 314 315#ifdef __BLOCKS__ 316 317/*! 318 @typedef AuthorizationAsyncCallback 319 Callback block passed to AuthorizationCopyRightsAsync. 320 321 @param err (output) The result of the AuthorizationCopyRights call. 322 @param blockAuthorizedRights (output) The authorizedRights from the AuthorizationCopyRights call to be deallocated by calling AuthorizationFreeItemSet() when it is no longer needed. 323*/ 324typedef void (^AuthorizationAsyncCallback)(OSStatus err, AuthorizationRights *blockAuthorizedRights); 325 326/*! 327 @function AuthorizationCopyRightsAsync 328 An asynchronous version of AuthorizationCopyRights. 329 330 @param callbackBlock (input) The callback block to be called upon completion. 331*/ 332void AuthorizationCopyRightsAsync(AuthorizationRef authorization, 333 const AuthorizationRights *rights, 334 const AuthorizationEnvironment *environment, 335 AuthorizationFlags flags, 336 AuthorizationAsyncCallback callbackBlock); 337 338 339#endif /* __BLOCKS__ */ 340 341 342/*! 343 @function AuthorizationCopyInfo 344 Returns sideband information (e.g. access credentials) obtained from a call to AuthorizationCreate. The format of this data depends of the tag specified. 345 346 @param authorization (input) The authorization object on which this operation is performed. 347 @param tag (input/optional) An optional string tag specifing which sideband information should be returned. When NULL is specified all available information is returned. 348 @param flags (input) options specified by the AuthorizationFlags enum. set all unused bits to zero to allow for future expansion. 349 @param info (output) A pointer to a newly allocated AuthorizationInfoSet in which the requested sideband infomation is returned (info should be deallocated by calling AuthorizationFreeItemSet() when it is no longer needed). 350 351 @result errAuthorizationSuccess 0 No error. 352 353 errAuthorizationInvalidRef -60002 The authorization parameter is invalid. 354 355 errAuthorizationInvalidTag -60003 The tag parameter is invalid. 356 357 errAuthorizationInvalidPointer -60004 The info parameter is invalid. 358*/ 359OSStatus AuthorizationCopyInfo(AuthorizationRef authorization, 360 AuthorizationString tag, 361 AuthorizationItemSet **info); 362 363 364/*! 365 @function AuthorizationMakeExternalForm 366 Turn an Authorization into an external "byte blob" form so it can be 367 transmitted to another process. 368 Note that *storing* the external form somewhere will probably not do what 369 you want, since authorizations are bounded by sessions, processes, and possibly 370 time limits. This is for online transmission of authorizations. 371 372 @param authorization The (valid) authorization reference to externalize 373 @param extForm Pointer to an AuthorizationExternalForm variable to fill. 374 375 @result errAuthorizationSuccess 0 No error. 376 377 errAuthorizationExternalizeNotAllowed -60009 Externalizing this authorization is not allowed. 378 379 errAuthorizationInvalidRef -60002 The authorization parameter is invalid. 380 381 382*/ 383OSStatus AuthorizationMakeExternalForm(AuthorizationRef authorization, 384 AuthorizationExternalForm *extForm); 385 386 387/*! 388 @function AuthorizationCreateFromExternalForm 389 Internalize the external "byte blob" form of an authorization reference. 390 391 @param extForm Pointer to an AuthorizationExternalForm value. 392 @param authorization Will be filled with a valid AuthorizationRef on success. 393 394 @result errAuthorizationInternalizeNotAllowed -60010 Internalizing this authorization is not allowed. 395*/ 396OSStatus AuthorizationCreateFromExternalForm(const AuthorizationExternalForm *extForm, 397 AuthorizationRef *authorization); 398 399 400/*! 401 @function AuthorizationFreeItemSet 402 Release the memory allocated for an AuthorizationItemSet that was allocated 403 by an API call. 404 405 @param set The AuthorizationItemSet to deallocate. 406 407 @result errAuthorizationSuccess 0 No error. 408 409 errAuthorizationInvalidSet -60001 The set parameter is invalid. 410*/ 411OSStatus AuthorizationFreeItemSet(AuthorizationItemSet *set); 412 413 414/*! 415 @function AuthorizationExecuteWithPrivileges 416 Run an executable tool with enhanced privileges after passing 417 suitable authorization procedures. 418 419 @param authorization An authorization reference that is used to authorize 420 access to the enhanced privileges. It is also passed to the tool for 421 further access control. 422 @param pathToTool Full pathname to the tool that should be executed 423 with enhanced privileges. 424 @param options Option bits (reserved). Must be zero. 425 @param arguments An argv-style vector of strings to be passed to the tool. 426 @param communicationsPipe Assigned a UNIX stdio FILE pointer for 427 a bidirectional pipe to communicate with the tool. The tool will have 428 this pipe as its standard I/O channels (stdin/stdout). If NULL, do not 429 establish a communications pipe. 430 431 @discussion This function has been deprecated and should no longer be used. 432 Use a launchd-launched helper tool and/or the Service Mangement framework 433 for this functionality. 434 */ 435OSStatus AuthorizationExecuteWithPrivileges(AuthorizationRef authorization, 436 const char *pathToTool, 437 AuthorizationFlags options, 438 char * const *arguments, 439 FILE **communicationsPipe) __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_1,__MAC_10_7,__IPHONE_NA,__IPHONE_NA); 440 441 442/*! 443 @function AuthorizationCopyPrivilegedReference 444 From within a tool launched via the AuthorizationExecuteWithPrivileges function 445 ONLY, retrieve the AuthorizationRef originally passed to that function. 446 While AuthorizationExecuteWithPrivileges already verified the authorization to 447 launch your tool, the tool may want to avail itself of any additional pre-authorizations 448 the caller may have obtained through that reference. 449 450 @discussion This function has been deprecated and should no longer be used. 451 Use a launchd-launched helper tool and/or the Service Mangement framework 452 for this functionality. 453 */ 454OSStatus AuthorizationCopyPrivilegedReference(AuthorizationRef *authorization, 455 AuthorizationFlags flags) __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_1,__MAC_10_7,__IPHONE_NA,__IPHONE_NA); 456 457 458#if defined(__cplusplus) 459} 460#endif 461 462#endif /* !_SECURITY_AUTHORIZATION_H_ */ 463