1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use core_foundation_sys::base::CFTypeRef;
use core_foundation_sys::base::OSStatus;
use core_foundation_sys::bundle::CFBundleRef;
use core_foundation_sys::dictionary::CFDictionaryRef;
use core_foundation_sys::string::CFStringRef;
use std::os::raw::{c_char, c_void};

pub const errAuthorizationSuccess: OSStatus = 0;
pub const errAuthorizationInvalidSet: OSStatus = -60001;
pub const errAuthorizationInvalidRef: OSStatus = -60002;
pub const errAuthorizationInvalidTag: OSStatus = -60003;
pub const errAuthorizationInvalidPointer: OSStatus = -60004;
pub const errAuthorizationDenied: OSStatus = -60005;
pub const errAuthorizationCanceled: OSStatus = -60006;
pub const errAuthorizationInteractionNotAllowed: OSStatus = -60007;
pub const errAuthorizationInternal: OSStatus = -60008;
pub const errAuthorizationExternalizeNotAllowed: OSStatus = -60009;
pub const errAuthorizationInternalizeNotAllowed: OSStatus = -60010;
pub const errAuthorizationInvalidFlags: OSStatus = -60011;
pub const errAuthorizationToolExecuteFailure: OSStatus = -60031;
pub const errAuthorizationToolEnvironmentError: OSStatus = -60032;
pub const errAuthorizationBadAddress: OSStatus = -60033;

pub type AuthorizationFlags = u32;
pub const kAuthorizationFlagDefaults: AuthorizationFlags = 0;
pub const kAuthorizationFlagInteractionAllowed: AuthorizationFlags = 1;
pub const kAuthorizationFlagExtendRights: AuthorizationFlags = 2;
pub const kAuthorizationFlagPartialRights: AuthorizationFlags = 4;
pub const kAuthorizationFlagDestroyRights: AuthorizationFlags = 8;
pub const kAuthorizationFlagPreAuthorize: AuthorizationFlags = 16;

pub type AuthorizationRef = *mut c_void;
pub type AuthorizationString = *const c_char;

#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct AuthorizationItem {
    pub name: AuthorizationString,
    pub valueLength: usize,
    pub value: *mut c_void,
    pub flags: u32,
}

#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct AuthorizationItemSet {
    pub count: u32,
    pub items: *mut AuthorizationItem,
}

pub const kAuthorizationExternalFormLength: usize = 32;

#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct AuthorizationExternalForm {
    pub bytes: [c_char; kAuthorizationExternalFormLength],
}

pub type AuthorizationRights = AuthorizationItemSet;
pub type AuthorizationEnvironment = AuthorizationItemSet;

pub type AuthorizationAsyncCallback =
    unsafe extern "C" fn(err: OSStatus, blockAuthorizedRights: *mut AuthorizationRights);

extern "C" {
    pub fn AuthorizationCreate(
        rights: *const AuthorizationRights,
        environment: *const AuthorizationEnvironment,
        flags: AuthorizationFlags,
        authorization: *mut AuthorizationRef,
    ) -> OSStatus;

    pub fn AuthorizationFree(
        authorization: AuthorizationRef,
        flags: AuthorizationFlags,
    ) -> OSStatus;

    pub fn AuthorizationCopyRights(
        authorization: AuthorizationRef,
        rights: *const AuthorizationRights,
        environment: *const AuthorizationEnvironment,
        flags: AuthorizationFlags,
        authorizedRights: *mut *mut AuthorizationRights,
    ) -> OSStatus;

    pub fn AuthorizationCopyRightsAsync(
        authorization: AuthorizationRef,
        rights: *const AuthorizationRights,
        environment: *const AuthorizationEnvironment,
        flags: AuthorizationFlags,
        callbackBlock: AuthorizationAsyncCallback,
    );

    pub fn AuthorizationCopyInfo(
        authorization: AuthorizationRef,
        tag: AuthorizationString,
        info: *mut *mut AuthorizationItemSet,
    ) -> OSStatus;

    pub fn AuthorizationMakeExternalForm(
        authorization: AuthorizationRef,
        extForm: *mut AuthorizationExternalForm,
    ) -> OSStatus;

    pub fn AuthorizationCreateFromExternalForm(
        extForm: *const AuthorizationExternalForm,
        authorization: *mut AuthorizationRef,
    ) -> OSStatus;

    pub fn AuthorizationFreeItemSet(set: *mut AuthorizationItemSet) -> OSStatus;

    pub fn AuthorizationRightGet(
        rightName: *const c_char,
        rightDefinition: *mut CFDictionaryRef,
    ) -> OSStatus;

    pub fn AuthorizationRightSet(
        authorization: AuthorizationRef,
        rightName: *const c_char,
        rightDefinition: CFTypeRef,
        descriptionKey: CFStringRef,
        bundle: CFBundleRef,
        localeTableName: CFStringRef,
    ) -> OSStatus;

    pub fn AuthorizationRightRemove(
        authorization: AuthorizationRef,
        rightName: *const c_char,
    ) -> OSStatus;

    #[cfg(target_os = "macos")]
    pub fn AuthorizationExecuteWithPrivileges(
        authorization: AuthorizationRef,
        pathToTool: *const c_char,
        options: AuthorizationFlags,
        arguments: *const *mut c_char,
        communicationsPipe: *mut *mut libc::FILE,
    ) -> OSStatus;

    #[cfg(target_os = "macos")]
    pub fn AuthorizationCopyPrivilegedReference(
        authorization: *mut AuthorizationRef,
        flags: AuthorizationFlags,
    ) -> OSStatus;
}