2 * Copyright (c) 2003-2010,2012,2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
26 #include "keychain_list.h"
28 #include "keychain_utilities.h"
33 #include <sys/param.h>
35 #include <CoreFoundation/CFArray.h>
36 #include <Security/SecKeychain.h>
38 // SecKeychainCopyLogin
39 #include <Security/SecKeychainPriv.h>
51 display_name(const void *value
, void *context
)
53 SecKeychainRef keychain
= (SecKeychainRef
)value
;
54 UInt32 pathLength
= MAXPATHLEN
;
55 char pathName
[MAXPATHLEN
+ 1];
56 OSStatus result
= SecKeychainGetPath(keychain
, &pathLength
, pathName
);
58 sec_error("SecKeychainGetPath %p: %s", keychain
, sec_errstr(result
));
60 fprintf(stdout
, " \"%*s\"\n", (int)pathLength
, pathName
);
65 display_list(const char *desc
, CFTypeRef keychainOrArray
)
67 if (desc
&& strlen(desc
))
68 fprintf(stdout
, "%s\n", desc
);
72 fprintf(stdout
, " <NULL>\n");
74 else if (CFGetTypeID(keychainOrArray
) == SecKeychainGetTypeID())
76 display_name(keychainOrArray
, NULL
);
80 CFArrayRef array
= (CFArrayRef
)keychainOrArray
;
81 CFRange range
= { 0, CFArrayGetCount(array
) };
82 CFArrayApplyFunction(array
, range
, display_name
, NULL
);
87 parse_domain(const char *name
, SecPreferencesDomain
*domain
)
89 size_t len
= strlen(name
);
91 if (!strncmp("user", name
, len
))
92 *domain
= kSecPreferencesDomainUser
;
93 else if (!strncmp("system", name
, len
))
94 *domain
= kSecPreferencesDomainSystem
;
95 else if (!strncmp("common", name
, len
))
96 *domain
= kSecPreferencesDomainCommon
;
97 else if (!strncmp("dynamic", name
, len
))
98 *domain
= kSecPreferencesDomainDynamic
;
101 sec_error("Invalid domain: %s", name
);
109 domain2str(SecPreferencesDomain domain
)
113 case kSecPreferencesDomainUser
:
115 case kSecPreferencesDomainSystem
:
117 case kSecPreferencesDomainCommon
:
119 case kSecPreferencesDomainDynamic
:
127 keychain_list(int argc
, char * const *argv
)
129 CFTypeRef keychainOrArray
= NULL
;
130 CFArrayRef searchList
= NULL
;
131 list_operation operation
= kList
;
132 SecPreferencesDomain domain
= kSecPreferencesDomainUser
;
133 Boolean use_domain
= false;
137 while ((ch
= getopt(argc
, argv
, "d:hs")) != -1)
142 result
= parse_domain(optarg
, &domain
);
152 return 2; /* @@@ Return 2 triggers usage message. */
169 result
= 2; // Show usage
174 status
= SecKeychainCopyDomainSearchList(domain
, &searchList
);
177 sec_error("SecKeychainCopyDomainSearchList %s: %s", domain2str(domain
), sec_errstr(status
));
183 fprintf(stdout
, "%s search list: ", domain2str(domain
));
185 display_list("", searchList
);
190 status
= SecKeychainCopySearchList(&searchList
);
193 sec_perror("SecKeychainCopySearchList", status
);
199 display_list("search list:", searchList
);
201 display_list("", searchList
);
208 keychainOrArray
= keychain_create_array(argc
, argv
);
210 searchList
= CFArrayCreate(NULL
, NULL
, 0, &kCFTypeArrayCallBacks
);
212 searchList
= CFArrayCreate(NULL
, &keychainOrArray
, 1, &kCFTypeArrayCallBacks
);
214 searchList
= (CFArrayRef
)CFRetain(keychainOrArray
);
218 status
= SecKeychainSetDomainSearchList(domain
, searchList
);
221 sec_error("SecKeychainSetDomainSearchList %s: %s", domain2str(domain
), sec_errstr(status
));
227 status
= SecKeychainSetSearchList(searchList
);
230 sec_perror("SecKeychainSetSearchList", status
);
238 CFRelease(keychainOrArray
);
240 CFRelease(searchList
);
246 keychain_default(int argc
, char * const *argv
)
248 SecPreferencesDomain domain
= kSecPreferencesDomainUser
;
249 SecKeychainRef keychain
= NULL
;
250 Boolean use_domain
= false;
251 Boolean do_set
= false;
255 while ((ch
= getopt(argc
, argv
, "d:hs")) != -1)
260 result
= parse_domain(optarg
, &domain
);
270 return 2; /* @@@ Return 2 triggers usage message. */
280 keychain
= (SecKeychainRef
)keychain_create_array(argc
, argv
);
286 status
= SecKeychainSetDomainDefault(domain
, keychain
);
289 sec_error("SecKeychainSetDomainDefault %s: %s", domain2str(domain
), sec_errstr(status
));
295 status
= SecKeychainSetDefault(keychain
);
298 sec_perror("SecKeychainSetDefault", status
);
310 status
= SecKeychainCopyDomainDefault(domain
, &keychain
);
313 sec_error("SecKeychainCopyDomainDefault %s: %s", domain2str(domain
), sec_errstr(status
));
319 fprintf(stdout
, "default %s keychain: ", domain2str(domain
));
321 display_list("", keychain
);
326 status
= SecKeychainCopyDefault(&keychain
);
329 sec_perror("SecKeychainCopyDefault", status
);
335 display_list("default keychain: ", keychain
);
337 display_list("", keychain
);
350 keychain_login(int argc
, char * const *argv
)
352 SecPreferencesDomain domain
= kSecPreferencesDomainUser
;
353 SecKeychainRef keychain
= NULL
;
354 Boolean use_domain
= false;
355 Boolean do_set
= false;
359 while ((ch
= getopt(argc
, argv
, "d:hs")) != -1)
364 result
= parse_domain(optarg
, &domain
);
374 return 2; /* @@@ Return 2 triggers usage message. */
384 keychain
= (SecKeychainRef
)keychain_create_array(argc
, argv
);
391 status
= SecKeychainSetDomainLogin(domain
, keychain
);
394 sec_error("SecKeychainSetDomainLogin %s: %s", domain2str(domain
), sec_errstr(status
));
400 status
= SecKeychainSetLogin(keychain
);
403 sec_perror("SecKeychainSetLogin", status
);
419 status
= SecKeychainCopyDomainLogin(domain
, &keychain
);
422 sec_error("SecKeychainCopyDomainLogin %s: %s", domain2str(domain
), sec_errstr(status
));
428 fprintf(stdout
, "login %s keychain: ", domain2str(domain
));
430 display_list("", keychain
);
438 status
= SecKeychainCopyLogin(&keychain
);
441 sec_perror("SecKeychainCopyLogin", status
);
447 display_list("login keychain: ", keychain
);
449 display_list("", keychain
);