]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSMacOSX/PreferencePane/ConfigurationAuthority.c
mDNSResponder-107.4.tar.gz
[apple/mdnsresponder.git] / mDNSMacOSX / PreferencePane / ConfigurationAuthority.c
1 /*
2 File: ConfigurationAuthority.c
3
4 Abstract: Interface to system security framework that manages access
5 to protected resources like system configuration preferences.
6
7 Copyright: (c) Copyright 2005 Apple Computer, Inc. All rights reserved.
8
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.
14
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.
29
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.
35
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.
43
44 Change History (most recent first):
45
46 $Log: ConfigurationAuthority.c,v $
47 Revision 1.1 2005/02/05 02:28:22 cheshire
48 Add Preference Pane to facilitate testing of DDNS & wide-area features
49
50 */
51
52 #include "ConfigurationAuthority.h"
53 #include "ConfigurationRights.h"
54
55 #include <AssertMacros.h>
56
57
58 static AuthorizationRef gAuthRef = 0;
59
60 static AuthorizationItem gAuthorizations[] = { { UPDATE_SC_RIGHT, 0, NULL, 0 },
61 { EDIT_SYS_KEYCHAIN_RIGHT, 0, NULL, 0 }};
62 static AuthorizationRights gAuthSet = { sizeof gAuthorizations / sizeof gAuthorizations[0], gAuthorizations };
63
64 static CFDictionaryRef CreateRightsDict( CFStringRef prompt)
65 /* Create a CFDictionary decribing an auth right. See /etc/authorization for examples. */
66 /* Specifies that the right requires admin authentication, which persists for 5 minutes. */
67 {
68 CFMutableDictionaryRef dict = NULL, tmpDict;
69 CFMutableArrayRef mechanisms;
70 CFNumberRef timeout;
71 int val;
72
73 tmpDict = CFDictionaryCreateMutable( (CFAllocatorRef) NULL, 0, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
74 require( tmpDict != NULL, MakeDictFailed);
75
76 CFDictionaryAddValue(tmpDict, CFSTR("class"), CFSTR("user"));
77 CFDictionaryAddValue(tmpDict, CFSTR("comment"), prompt);
78 CFDictionaryAddValue(tmpDict, CFSTR("group"), CFSTR("admin"));
79
80 mechanisms = CFArrayCreateMutable((CFAllocatorRef) NULL, 1, &kCFTypeArrayCallBacks);
81 require( mechanisms != NULL, MakeArrayFailed);
82 CFArrayAppendValue( mechanisms, CFSTR("builtin:authenticate"));
83 CFDictionaryAddValue( tmpDict, CFSTR("mechanisms"), mechanisms);
84
85 val = 300; // seconds
86 timeout = CFNumberCreate((CFAllocatorRef) NULL, kCFNumberIntType, &val);
87 require( timeout != NULL, MakeIntFailed);
88 CFDictionaryAddValue( tmpDict, CFSTR("timeout"), timeout);
89 CFDictionaryAddValue( tmpDict, CFSTR("shared"), kCFBooleanTrue);
90
91 dict = tmpDict;
92 tmpDict = NULL;
93
94 CFRelease( timeout);
95 MakeIntFailed:
96 CFRelease( mechanisms);
97 MakeArrayFailed:
98 if ( tmpDict)
99 CFRelease( tmpDict);
100 MakeDictFailed:
101 return dict;
102 }
103
104 OSStatus InitConfigAuthority(void)
105 /* Initialize the authorization record-keeping */
106 {
107 OSStatus err;
108 CFDictionaryRef dict;
109 CFStringRef rightInfo;
110
111 err = AuthorizationCreate((AuthorizationRights*) NULL, (AuthorizationEnvironment*) NULL,
112 (AuthorizationFlags) 0, &gAuthRef);
113 require_noerr( err, NewAuthFailed);
114
115 err = AuthorizationRightGet( UPDATE_SC_RIGHT, (CFDictionaryRef*) NULL);
116 if( err == errAuthorizationDenied)
117 {
118 rightInfo = CFCopyLocalizedString(CFSTR("Authentication required to set Dynamic DNS preferences."),
119 CFSTR("Describes operation that requires user authorization"));
120 require_action( rightInfo != NULL, GetStrFailed, err=coreFoundationUnknownErr;);
121 dict = CreateRightsDict(rightInfo);
122 require_action( dict != NULL, GetStrFailed, err=coreFoundationUnknownErr;);
123
124 err = AuthorizationRightSet(gAuthRef, UPDATE_SC_RIGHT, dict, (CFStringRef) NULL,
125 (CFBundleRef) NULL, (CFStringRef) NULL);
126 CFRelease(rightInfo);
127 CFRelease(dict);
128 }
129 require_noerr( err, AuthSetFailed);
130
131 err = AuthorizationRightGet( EDIT_SYS_KEYCHAIN_RIGHT, (CFDictionaryRef*) NULL);
132 if( err == errAuthorizationDenied)
133 {
134 rightInfo = CFCopyLocalizedString( CFSTR("Authentication required to edit System Keychain."),
135 CFSTR("Describes operation that requires user authorization"));
136 require_action( rightInfo != NULL, GetStrFailed, err=coreFoundationUnknownErr;);
137 dict = CreateRightsDict( rightInfo);
138 require_action( dict != NULL, GetStrFailed, err=coreFoundationUnknownErr;);
139
140 err = AuthorizationRightSet(gAuthRef, EDIT_SYS_KEYCHAIN_RIGHT, dict, (CFStringRef) NULL,
141 (CFBundleRef) NULL, (CFStringRef) NULL);
142 CFRelease( rightInfo);
143 CFRelease( dict);
144 }
145 require_noerr( err, AuthSetFailed);
146
147 (void) AttemptAcquireAuthority( false);
148
149 AuthSetFailed:
150 GetStrFailed:
151 NewAuthFailed:
152 return err;
153 }
154
155 OSStatus AttemptAcquireAuthority( Boolean allowUI)
156 /* Try to get permission for privileged ops, either implicitly or by asking the user for */
157 /* authority to perform operations (if necessary) */
158 {
159 AuthorizationFlags allowFlag = allowUI ? kAuthorizationFlagInteractionAllowed : 0;
160 OSStatus err;
161
162 err = AuthorizationCopyRights( gAuthRef, &gAuthSet, (AuthorizationEnvironment*) NULL,
163 kAuthorizationFlagExtendRights | kAuthorizationFlagPreAuthorize |
164 allowFlag,
165 (AuthorizationRights**) NULL);
166 return err;
167 }
168
169 OSStatus ReleaseAuthority(void)
170 /* Discard authority to perform operations */
171 {
172 (void) AuthorizationFree( gAuthRef, kAuthorizationFlagDestroyRights);
173 gAuthRef = 0;
174 return AuthorizationCreate( (AuthorizationRights*) NULL, (AuthorizationEnvironment*) NULL,
175 (AuthorizationFlags) 0, &gAuthRef);
176 }
177
178 Boolean CurrentlyAuthorized(void)
179 {
180 OSStatus err;
181
182 err = AuthorizationCopyRights(gAuthRef, &gAuthSet, (AuthorizationEnvironment*) NULL,
183 (AuthorizationFlags) 0, (AuthorizationRights**) NULL);
184
185 return err == noErr;
186 }
187
188
189 OSStatus ExternalizeAuthority(AuthorizationExternalForm *pAuth)
190 /* Package up current authorizations for transfer to another process */
191 {
192 return AuthorizationMakeExternalForm(gAuthRef, pAuth);
193 }