;;; Copyright (c) 2017 Apple Inc.  All Rights reserved.
;;;
;;; WARNING: The sandbox rules in this file currently constitute
;;; Apple System Private Interface and are subject to change at any time and
;;; without notice.
;;;
(version 1)

(deny default)
(deny file-map-executable process-info* nvram*)
(deny dynamic-code-generation)

(deny mach-priv-host-port)

(import "system.sb")
(import "com.apple.corefoundation.sb")
(corefoundation)

;;; Homedir-relative path filters
(define (home-regex home-relative-regex)
    (regex (string-append "^" (regex-quote (param "HOME")) home-relative-regex)))

(define (home-subpath home-relative-subpath)
    (subpath (string-append (param "HOME") home-relative-subpath)))

(define (home-prefix home-relative-prefix)
    (prefix (string-append (param "HOME") home-relative-prefix)))

(define (home-literal home-relative-literal)
    (literal (string-append (param "HOME") home-relative-literal)))


(allow process-info* (target self))

;; For resolving symlinks, realpath(3), and equivalents.
(allow file-read-metadata)

;; For validating the entitlements of clients.
(allow process-info-codesignature)

(allow user-preference-read user-preference-write
    (preference-domain "com.apple.callintelligenced"))

;; Read
(allow file-read* file-test-existence
    (literal "/Library/Preferences/com.apple.networkd.plist")
    (literal "/Library/Preferences/com.apple.networkextension.uuidcache.plist")
)


;; Read/write
(allow file-read* file-write*
    (subpath (param "TMPDIR"))
    (subpath (param "DARWIN_CACHE_DIR"))
    (home-subpath "/Library/Application Support/com.apple.callintelligenced")
    (home-subpath "/Library/Caches/com.apple.callintelligenced")
)

;; Mach lookups
(allow mach-lookup
    (global-name "com.apple.accountsd.accountmanager")
    (global-name "com.apple.AddressBook.abd")
    (global-name "com.apple.AddressBook.ContactsAccountsService")
    (global-name "com.apple.apsd")
    (global-name "com.apple.cloudd")
    (global-name "com.apple.contacts.account-caching")
    (global-name "com.apple.contactsd.persistence")
    (global-name "com.apple.identityservicesd.desktop.auth")
    (global-name "com.apple.logind")
    (global-name "com.apple.lsd.mapdb")
    (global-name "com.apple.metadata.mds")
    (global-name "com.apple.nehelper")
    (global-name "com.apple.system.opendirectoryd.api")
    (global-name "com.apple.tccd.system")
    (global-name "com.apple.tccd")
    (global-name "com.apple.usernotifications.listener")
    (global-name "com.apple.windowserver.active")
    (global-name "com.apple.containermanagerd")
    (global-name "com.apple.spotlight.IndexAgent")
    (global-name "com.apple.telephonyutilities.callservicesdaemon.callstatecontroller")
    (global-name "com.apple.coremedia.systemcontroller.xpc")
    (global-name "com.apple.speech.localspeechrecognition")
    (global-name "com.apple.videoconference.camera")
    (global-name "com.apple.telephonyutilities.callservicesdaemon.conversationmanager")
    (global-name "com.apple.telephonyutilities.callservicesdaemon.callcapabilities")
    (global-name "com.apple.telephonyutilities.callservicesdaemon.conversationprovidermanager")
    (global-name "com.apple.identityservicesd.idquery.desktop.auth")
    (global-name "com.apple.mobileassetd.v2")
    (global-name "com.apple.gpumemd.source")
    (global-name "com.apple.sirittsd")
    (global-name "com.apple.symptom_diagnostics")
    (global-name "com.apple.naturallanguaged")
    (global-name "com.apple.mobileasset.autoasset")
    (global-name "mul-xpc (Apple)_OpenStep")
    (global-name "com.apple.powerlog.plxpclogger.xpc")
    (global-name "com.apple.PerfPowerTelemetryClientRegistrationService")
)

(allow iokit-open
    (iokit-registry-entry-class "RootDomainUserClient")
)

(allow file-map-executable
    (subpath "/System/Library/Address Book Plug-Ins")
    (subpath "/System/Library/Video/Plug-Ins/")
)

(allow file-read* file-write*
    (subpath (param "TMPDIR"))
    (subpath (param "DARWIN_CACHE_DIR"))
    (extension "com.apple.sandbox.application-group")
    (extension "com.apple.app-sandbox.read"))

; Support for issuing extensions in our temporary directories.
(allow file-issue-extension
    (require-all
        (extension-class "com.apple.app-sandbox.read" "com.apple.app-sandbox.read-write")
        (require-any
            (subpath (param "TMPDIR"))
            (subpath (param "DARWIN_CACHE_DIR"))
            (extension "com.apple.sandbox.application-group")
            (home-subpath "/Library/Application Support/com.apple.callintelligenced")
        )
    )
)

; Support for issuing extensions for framework resources
(allow file-issue-extension
       (require-all
           (extension-class "com.apple.app-sandbox.read")
           (subpath "/System/Library/PrivateFrameworks/CallIntelligence.framework")
       )
)

(allow user-preference-write
    (preference-domain "com.apple.TelephonyUtilities")
)

(allow user-preference-read
    (preference-domain "com.apple.CloudKit")
    (preference-domain "com.apple.TelephonyUtilities")
    (preference-domain "com.apple.ids")
    (preference-domain "com.apple.AddressBook")
    (preference-domain "com.apple.facetime.bag")
    (preference-domain "kCFPreferencesAnyApplication")
    (preference-domain "com.apple.assistant.support")
    (preference-domain "com.apple.voiceservices")
    (preference-domain "com.apple.facetime.bag")
)

(allow file-read* (extension "com.apple.app-sandbox.read"))

(allow iokit-open-user-client
    (iokit-user-client-class "IOSurfaceRootUserClient")
    (iokit-user-client-class "AGXDeviceUserClient")
)

(define (home-subpath home-relative-subpath)
    (subpath (string-append (param "HOME") home-relative-subpath)))

;; Allow read-only access to $HOME/Library/Trial
(allow file-read*
    (home-subpath "/Library/Trial"))
