]> git.saurik.com Git - apple/security.git/blob - OSX/sec/SOSCircle/Tool/secViewDisplay.c
Security-57740.31.2.tar.gz
[apple/security.git] / OSX / sec / SOSCircle / Tool / secViewDisplay.c
1 /*
2 * Copyright (c) 2013-2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 // secViewDisplay.c
25 // sec
26 //
27 //
28 //
29
30 #include "secViewDisplay.h"
31 #include "secToolFileIO.h"
32
33 #include <Security/SecureObjectSync/SOSCloudCircle.h>
34 #include <Security/SecureObjectSync/SOSCloudCircleInternal.h>
35 #include <Security/SecureObjectSync/SOSViews.h>
36
37
38 static struct foo {
39 const char *name;
40 const CFStringRef *viewspec;
41 } string2View[] = {
42 { "keychain", &kSOSViewKeychainV0 },
43 #undef DOVIEWMACRO
44 #define DOVIEWMACRO(VIEWNAME, DEFSTRING, CMDSTRING, DEFAULTSETTING, INITIALSYNCSETTING, ALWAYSONSETTING, BACKUPSETTING, V0SETTING) { CMDSTRING, &kSOSView##VIEWNAME, },
45 #include "Security/SecureObjectSync/ViewList.list"
46 };
47
48 static CFStringRef convertStringToView(char *viewname) {
49 unsigned n;
50
51 for (n = 0; n < sizeof(string2View)/sizeof(string2View[0]); n++) {
52 if (strcmp(string2View[n].name, viewname) == 0)
53 return *string2View[n].viewspec;
54 }
55
56 // Leak this, since it's a getter.
57 return CFStringCreateWithCString(kCFAllocatorDefault, viewname, kCFStringEncodingUTF8);
58 }
59
60 static CFStringRef convertViewReturnCodeToString(SOSViewActionCode ac) {
61 CFStringRef retval = NULL;
62 switch(ac) {
63 case kSOSCCGeneralViewError:
64 retval = CFSTR("General Error"); break;
65 case kSOSCCViewMember:
66 retval = CFSTR("Is Member of View"); break;
67 case kSOSCCViewNotMember:
68 retval = CFSTR("Is Not Member of View"); break;
69 case kSOSCCViewNotQualified:
70 retval = CFSTR("Is not qualified for View"); break;
71 case kSOSCCNoSuchView:
72 retval = CFSTR("No Such View"); break;
73 }
74 return retval;
75 }
76
77 bool viewcmd(char *itemName, CFErrorRef *err) {
78 char *cmd, *viewname;
79 SOSViewActionCode ac = kSOSCCViewQuery;
80 CFStringRef viewspec;
81
82 viewname = strchr(itemName, ':');
83 if(viewname == NULL) return false;
84 *viewname = 0;
85 viewname++;
86 cmd = itemName;
87
88 if(strcmp(cmd, "enable") == 0) {
89 ac = kSOSCCViewEnable;
90 } else if(strcmp(cmd, "disable") == 0) {
91 ac = kSOSCCViewDisable;
92 } else if(strcmp(cmd, "query") == 0) {
93 ac = kSOSCCViewQuery;
94 } else {
95 return false;
96 }
97
98 if(strchr(viewname, ',') == NULL) { // original single value version
99 viewspec = convertStringToView(viewname);
100 if(!viewspec) return false;
101
102 SOSViewResultCode rc = SOSCCView(viewspec, ac, err);
103 CFStringRef resultString = convertViewReturnCodeToString(rc);
104
105 printmsg(CFSTR("View Result: %@ : %@\n"), resultString, viewspec);
106 return true;
107 }
108
109 if(ac == kSOSCCViewQuery) return false;
110
111 // new multi-view version
112 char *viewlist = strdup(viewname);
113 char *token;
114 char *tofree = viewlist;
115 CFMutableSetRef viewSet = CFSetCreateMutable(NULL, 0, &kCFCopyStringSetCallBacks);
116
117 while ((token = strsep(&viewlist, ",")) != NULL) {
118 CFStringRef resultString = convertStringToView(token);
119 CFSetAddValue(viewSet, resultString);
120 }
121
122 printmsg(CFSTR("viewSet provided is %@\n"), viewSet);
123
124 free(tofree);
125
126 bool retcode;
127 if(ac == kSOSCCViewEnable) retcode = SOSCCViewSet(viewSet, NULL);
128 else retcode = SOSCCViewSet(NULL, viewSet);
129
130 fprintf(outFile, "SOSCCViewSet returned %s\n", (retcode)? "true": "false");
131
132 return true;
133 }
134
135 bool listviewcmd(CFErrorRef *err) {
136 unsigned n;
137
138 for (n = 0; n < sizeof(string2View)/sizeof(string2View[0]); n++) {
139 CFStringRef viewspec = *string2View[n].viewspec;
140
141 SOSViewResultCode rc = SOSCCView(viewspec, kSOSCCViewQuery, err);
142 CFStringRef resultString = convertViewReturnCodeToString(rc);
143
144 printmsg(CFSTR("View Result: %@ : %@\n"), resultString, viewspec);
145 };
146
147 return true;
148 }