/ keychain / SecureObjectSync / Tool / secViewDisplay.c
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, SYSTEM, DEFAULTSETTING, INITIALSYNCSETTING, ALWAYSONSETTING, BACKUPSETTING, V0SETTING) \
 45      { CMDSTRING, &k##SYSTEM##View##VIEWNAME, },
 46  #include "keychain/SecureObjectSync/ViewList.list"
 47  };
 48  
 49  static CFStringRef convertStringToView(char *viewname) {
 50      unsigned n;
 51      
 52      for (n = 0; n < sizeof(string2View)/sizeof(string2View[0]); n++) {
 53          if (strcmp(string2View[n].name, viewname) == 0)
 54              return *string2View[n].viewspec;
 55      }
 56      
 57      // Leak this, since it's a getter.
 58      return CFStringCreateWithCString(kCFAllocatorDefault, viewname, kCFStringEncodingUTF8);
 59  }
 60  
 61  static CFStringRef convertViewReturnCodeToString(SOSViewActionCode ac) {
 62      CFStringRef retval = NULL;
 63      switch(ac) {
 64          case kSOSCCGeneralViewError:
 65              retval = CFSTR("General Error"); break;
 66          case kSOSCCViewMember:
 67              retval = CFSTR("Is Member of View"); break;
 68          case kSOSCCViewNotMember:
 69              retval = CFSTR("Is Not Member of View"); break;
 70          case kSOSCCViewNotQualified:
 71              retval = CFSTR("Is not qualified for View"); break;
 72          case kSOSCCNoSuchView:
 73              retval = CFSTR("No Such View"); break;
 74      }
 75      return retval;
 76  }
 77  
 78  bool viewcmd(char *itemName, CFErrorRef *err) {
 79      char *cmd, *viewname;
 80      SOSViewActionCode ac = kSOSCCViewQuery;
 81      CFStringRef viewspec;
 82      
 83      viewname = strchr(itemName, ':');
 84      if(viewname == NULL) return false;
 85      *viewname = 0;
 86      viewname++;
 87      cmd = itemName;
 88      
 89      if(strcmp(cmd, "enable") == 0) {
 90          ac = kSOSCCViewEnable;
 91      } else if(strcmp(cmd, "disable") == 0) {
 92          ac = kSOSCCViewDisable;
 93      } else if(strcmp(cmd, "query") == 0) {
 94          ac = kSOSCCViewQuery;
 95      } else {
 96          return false;
 97      }
 98      
 99      if(strchr(viewname, ',') == NULL) { // original single value version
100          viewspec = convertStringToView(viewname);
101          if(!viewspec) return false;
102          
103          SOSViewResultCode rc = SOSCCView(viewspec, ac, err);
104          CFStringRef resultString = convertViewReturnCodeToString(rc);
105          
106          printmsg(CFSTR("View Result: %@ : %@\n"), resultString, viewspec);
107          return true;
108      }
109      
110      if(ac == kSOSCCViewQuery) return false;
111      
112      // new multi-view version
113      char *viewlist = strdup(viewname);
114      char *token;
115      char *tofree = viewlist;
116      CFMutableSetRef viewSet = CFSetCreateMutable(NULL, 0, &kCFCopyStringSetCallBacks);
117      
118      while ((token = strsep(&viewlist, ",")) != NULL) {
119          CFStringRef resultString = convertStringToView(token);
120          CFSetAddValue(viewSet, resultString);
121      }
122      
123      printmsg(CFSTR("viewSet provided is %@\n"), viewSet);
124      
125      free(tofree);
126      
127      bool retcode;
128      if(ac == kSOSCCViewEnable) retcode = SOSCCViewSet(viewSet, NULL);
129      else retcode = SOSCCViewSet(NULL, viewSet);
130      
131      fprintf(outFile, "SOSCCViewSet returned %s\n", (retcode)? "true": "false");
132      
133      return true;
134  }
135  
136  bool listviewcmd(CFErrorRef *err) {
137      unsigned n;
138      
139      for (n = 0; n < sizeof(string2View)/sizeof(string2View[0]); n++) {
140          CFStringRef viewspec = *string2View[n].viewspec;
141          
142          SOSViewResultCode rc = SOSCCView(viewspec, kSOSCCViewQuery, err);
143          CFStringRef resultString = convertViewReturnCodeToString(rc);
144          
145          printmsg(CFSTR("View Result: %@ : %@\n"), resultString, viewspec);
146      };
147      
148      return true;
149  }