2 File: ConfigurationAuthority.c
4 Abstract: Interface to system security framework that manages access
5 to protected resources like system configuration preferences.
7 Copyright: (c) Copyright 2005 Apple Computer, Inc. All rights reserved.
9 Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
10 ("Apple") in consideration of your agreement to the following terms, and your
11 use, installation, modification or redistribution of this Apple software
12 constitutes acceptance of these terms. If you do not agree with these terms,
13 please do not use, install, modify or redistribute this Apple software.
15 In consideration of your agreement to abide by the following terms, and subject
16 to these terms, Apple grants you a personal, non-exclusive license, under Apple's
17 copyrights in this original Apple software (the "Apple Software"), to use,
18 reproduce, modify and redistribute the Apple Software, with or without
19 modifications, in source and/or binary forms; provided that if you redistribute
20 the Apple Software in its entirety and without modifications, you must retain
21 this notice and the following text and disclaimers in all such redistributions of
22 the Apple Software. Neither the name, trademarks, service marks or logos of
23 Apple Computer, Inc. may be used to endorse or promote products derived from the
24 Apple Software without specific prior written permission from Apple. Except as
25 expressly stated in this notice, no other rights or licenses, express or implied,
26 are granted by Apple herein, including but not limited to any patent rights that
27 may be infringed by your derivative works or by other works in which the Apple
28 Software may be incorporated.
30 The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
31 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
32 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
34 COMBINATION WITH YOUR PRODUCTS.
36 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
37 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
38 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
40 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
41 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
42 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 Change History (most recent first):
46 $Log: ConfigurationAuthority.c,v $
47 Revision 1.3 2008/06/26 17:34:18 mkrochma
48 <rdar://problem/6030630> Pref pane destroying shared "system.preferences" authorization right
50 Revision 1.2 2005/08/07 22:48:05 mkrochma
51 <rdar://problem/4204003> Bonjour Pref Pane returns -927 when "system.preferences" is not shared
53 Revision 1.1 2005/02/05 02:28:22 cheshire
54 Add Preference Pane to facilitate testing of DDNS & wide-area features
58 #include "ConfigurationAuthority.h"
59 #include "ConfigurationRights.h"
61 #include <AssertMacros.h>
64 static AuthorizationRef gAuthRef
= 0;
66 static AuthorizationItem gAuthorizations
[] = { { UPDATE_SC_RIGHT
, 0, NULL
, 0 },
67 { EDIT_SYS_KEYCHAIN_RIGHT
, 0, NULL
, 0 }};
68 static AuthorizationRights gAuthSet
= { sizeof gAuthorizations
/ sizeof gAuthorizations
[0], gAuthorizations
};
70 static CFDictionaryRef
CreateRightsDict( CFStringRef prompt
)
71 /* Create a CFDictionary decribing an auth right. See /etc/authorization for examples. */
72 /* Specifies that the right requires admin authentication, which persists for 5 minutes. */
74 CFMutableDictionaryRef dict
= NULL
, tmpDict
;
75 CFMutableArrayRef mechanisms
;
79 tmpDict
= CFDictionaryCreateMutable( (CFAllocatorRef
) NULL
, 0, &kCFCopyStringDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
80 require( tmpDict
!= NULL
, MakeDictFailed
);
82 CFDictionaryAddValue(tmpDict
, CFSTR("class"), CFSTR("user"));
83 CFDictionaryAddValue(tmpDict
, CFSTR("comment"), prompt
);
84 CFDictionaryAddValue(tmpDict
, CFSTR("group"), CFSTR("admin"));
86 mechanisms
= CFArrayCreateMutable((CFAllocatorRef
) NULL
, 1, &kCFTypeArrayCallBacks
);
87 require( mechanisms
!= NULL
, MakeArrayFailed
);
88 CFArrayAppendValue( mechanisms
, CFSTR("builtin:authenticate"));
89 CFDictionaryAddValue( tmpDict
, CFSTR("mechanisms"), mechanisms
);
92 timeout
= CFNumberCreate((CFAllocatorRef
) NULL
, kCFNumberIntType
, &val
);
93 require( timeout
!= NULL
, MakeIntFailed
);
94 CFDictionaryAddValue( tmpDict
, CFSTR("timeout"), timeout
);
95 CFDictionaryAddValue( tmpDict
, CFSTR("shared"), kCFBooleanTrue
);
102 CFRelease( mechanisms
);
110 OSStatus
InitConfigAuthority(void)
111 /* Initialize the authorization record-keeping */
114 CFDictionaryRef dict
;
115 CFStringRef rightInfo
;
117 err
= AuthorizationCreate((AuthorizationRights
*) NULL
, (AuthorizationEnvironment
*) NULL
,
118 (AuthorizationFlags
) 0, &gAuthRef
);
119 require_noerr( err
, NewAuthFailed
);
121 err
= AuthorizationRightGet( UPDATE_SC_RIGHT
, (CFDictionaryRef
*) NULL
);
122 if( err
== errAuthorizationDenied
)
124 rightInfo
= CFCopyLocalizedString(CFSTR("Authentication required to set Dynamic DNS preferences."),
125 CFSTR("Describes operation that requires user authorization"));
126 require_action( rightInfo
!= NULL
, GetStrFailed
, err
=coreFoundationUnknownErr
;);
127 dict
= CreateRightsDict(rightInfo
);
128 require_action( dict
!= NULL
, GetStrFailed
, err
=coreFoundationUnknownErr
;);
130 err
= AuthorizationRightSet(gAuthRef
, UPDATE_SC_RIGHT
, dict
, (CFStringRef
) NULL
,
131 (CFBundleRef
) NULL
, (CFStringRef
) NULL
);
132 CFRelease(rightInfo
);
135 require_noerr( err
, AuthSetFailed
);
137 err
= AuthorizationRightGet( EDIT_SYS_KEYCHAIN_RIGHT
, (CFDictionaryRef
*) NULL
);
138 if( err
== errAuthorizationDenied
)
140 rightInfo
= CFCopyLocalizedString( CFSTR("Authentication required to edit System Keychain."),
141 CFSTR("Describes operation that requires user authorization"));
142 require_action( rightInfo
!= NULL
, GetStrFailed
, err
=coreFoundationUnknownErr
;);
143 dict
= CreateRightsDict( rightInfo
);
144 require_action( dict
!= NULL
, GetStrFailed
, err
=coreFoundationUnknownErr
;);
146 err
= AuthorizationRightSet(gAuthRef
, EDIT_SYS_KEYCHAIN_RIGHT
, dict
, (CFStringRef
) NULL
,
147 (CFBundleRef
) NULL
, (CFStringRef
) NULL
);
148 CFRelease( rightInfo
);
151 require_noerr( err
, AuthSetFailed
);
159 OSStatus
AttemptAcquireAuthority( Boolean allowUI
)
160 /* Try to get permission for privileged ops, either implicitly or by asking the user for */
161 /* authority to perform operations (if necessary) */
163 AuthorizationFlags allowFlag
= allowUI
? kAuthorizationFlagInteractionAllowed
: 0;
166 err
= AuthorizationCopyRights( gAuthRef
, &gAuthSet
, (AuthorizationEnvironment
*) NULL
,
167 kAuthorizationFlagExtendRights
| kAuthorizationFlagPreAuthorize
|
169 (AuthorizationRights
**) NULL
);
173 OSStatus
ReleaseAuthority(void)
174 /* Discard authority to perform operations */
176 (void) AuthorizationFree( gAuthRef
, kAuthorizationFlagDefaults
);
178 return AuthorizationCreate( (AuthorizationRights
*) NULL
, (AuthorizationEnvironment
*) NULL
,
179 (AuthorizationFlags
) 0, &gAuthRef
);
182 Boolean
CurrentlyAuthorized(void)
184 OSStatus err
= AttemptAcquireAuthority(true);
189 OSStatus
ExternalizeAuthority(AuthorizationExternalForm
*pAuth
)
190 /* Package up current authorizations for transfer to another process */
192 return AuthorizationMakeExternalForm(gAuthRef
, pAuth
);