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.2 2005/08/07 22:48:05 mkrochma
48 <rdar://problem/4204003> Bonjour Pref Pane returns -927 when "system.preferences" is not shared
50 Revision 1.1 2005/02/05 02:28:22 cheshire
51 Add Preference Pane to facilitate testing of DDNS & wide-area features
55 #include "ConfigurationAuthority.h"
56 #include "ConfigurationRights.h"
58 #include <AssertMacros.h>
61 static AuthorizationRef gAuthRef
= 0;
63 static AuthorizationItem gAuthorizations
[] = { { UPDATE_SC_RIGHT
, 0, NULL
, 0 },
64 { EDIT_SYS_KEYCHAIN_RIGHT
, 0, NULL
, 0 }};
65 static AuthorizationRights gAuthSet
= { sizeof gAuthorizations
/ sizeof gAuthorizations
[0], gAuthorizations
};
67 static CFDictionaryRef
CreateRightsDict( CFStringRef prompt
)
68 /* Create a CFDictionary decribing an auth right. See /etc/authorization for examples. */
69 /* Specifies that the right requires admin authentication, which persists for 5 minutes. */
71 CFMutableDictionaryRef dict
= NULL
, tmpDict
;
72 CFMutableArrayRef mechanisms
;
76 tmpDict
= CFDictionaryCreateMutable( (CFAllocatorRef
) NULL
, 0, &kCFCopyStringDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
77 require( tmpDict
!= NULL
, MakeDictFailed
);
79 CFDictionaryAddValue(tmpDict
, CFSTR("class"), CFSTR("user"));
80 CFDictionaryAddValue(tmpDict
, CFSTR("comment"), prompt
);
81 CFDictionaryAddValue(tmpDict
, CFSTR("group"), CFSTR("admin"));
83 mechanisms
= CFArrayCreateMutable((CFAllocatorRef
) NULL
, 1, &kCFTypeArrayCallBacks
);
84 require( mechanisms
!= NULL
, MakeArrayFailed
);
85 CFArrayAppendValue( mechanisms
, CFSTR("builtin:authenticate"));
86 CFDictionaryAddValue( tmpDict
, CFSTR("mechanisms"), mechanisms
);
89 timeout
= CFNumberCreate((CFAllocatorRef
) NULL
, kCFNumberIntType
, &val
);
90 require( timeout
!= NULL
, MakeIntFailed
);
91 CFDictionaryAddValue( tmpDict
, CFSTR("timeout"), timeout
);
92 CFDictionaryAddValue( tmpDict
, CFSTR("shared"), kCFBooleanTrue
);
99 CFRelease( mechanisms
);
107 OSStatus
InitConfigAuthority(void)
108 /* Initialize the authorization record-keeping */
111 CFDictionaryRef dict
;
112 CFStringRef rightInfo
;
114 err
= AuthorizationCreate((AuthorizationRights
*) NULL
, (AuthorizationEnvironment
*) NULL
,
115 (AuthorizationFlags
) 0, &gAuthRef
);
116 require_noerr( err
, NewAuthFailed
);
118 err
= AuthorizationRightGet( UPDATE_SC_RIGHT
, (CFDictionaryRef
*) NULL
);
119 if( err
== errAuthorizationDenied
)
121 rightInfo
= CFCopyLocalizedString(CFSTR("Authentication required to set Dynamic DNS preferences."),
122 CFSTR("Describes operation that requires user authorization"));
123 require_action( rightInfo
!= NULL
, GetStrFailed
, err
=coreFoundationUnknownErr
;);
124 dict
= CreateRightsDict(rightInfo
);
125 require_action( dict
!= NULL
, GetStrFailed
, err
=coreFoundationUnknownErr
;);
127 err
= AuthorizationRightSet(gAuthRef
, UPDATE_SC_RIGHT
, dict
, (CFStringRef
) NULL
,
128 (CFBundleRef
) NULL
, (CFStringRef
) NULL
);
129 CFRelease(rightInfo
);
132 require_noerr( err
, AuthSetFailed
);
134 err
= AuthorizationRightGet( EDIT_SYS_KEYCHAIN_RIGHT
, (CFDictionaryRef
*) NULL
);
135 if( err
== errAuthorizationDenied
)
137 rightInfo
= CFCopyLocalizedString( CFSTR("Authentication required to edit System Keychain."),
138 CFSTR("Describes operation that requires user authorization"));
139 require_action( rightInfo
!= NULL
, GetStrFailed
, err
=coreFoundationUnknownErr
;);
140 dict
= CreateRightsDict( rightInfo
);
141 require_action( dict
!= NULL
, GetStrFailed
, err
=coreFoundationUnknownErr
;);
143 err
= AuthorizationRightSet(gAuthRef
, EDIT_SYS_KEYCHAIN_RIGHT
, dict
, (CFStringRef
) NULL
,
144 (CFBundleRef
) NULL
, (CFStringRef
) NULL
);
145 CFRelease( rightInfo
);
148 require_noerr( err
, AuthSetFailed
);
156 OSStatus
AttemptAcquireAuthority( Boolean allowUI
)
157 /* Try to get permission for privileged ops, either implicitly or by asking the user for */
158 /* authority to perform operations (if necessary) */
160 AuthorizationFlags allowFlag
= allowUI
? kAuthorizationFlagInteractionAllowed
: 0;
163 err
= AuthorizationCopyRights( gAuthRef
, &gAuthSet
, (AuthorizationEnvironment
*) NULL
,
164 kAuthorizationFlagExtendRights
| kAuthorizationFlagPreAuthorize
|
166 (AuthorizationRights
**) NULL
);
170 OSStatus
ReleaseAuthority(void)
171 /* Discard authority to perform operations */
173 (void) AuthorizationFree( gAuthRef
, kAuthorizationFlagDestroyRights
);
175 return AuthorizationCreate( (AuthorizationRights
*) NULL
, (AuthorizationEnvironment
*) NULL
,
176 (AuthorizationFlags
) 0, &gAuthRef
);
179 Boolean
CurrentlyAuthorized(void)
181 OSStatus err
= AttemptAcquireAuthority(true);
186 OSStatus
ExternalizeAuthority(AuthorizationExternalForm
*pAuth
)
187 /* Package up current authorizations for transfer to another process */
189 return AuthorizationMakeExternalForm(gAuthRef
, pAuth
);