;;; 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 "contacts.sb")
(import "com.apple.corefoundation.sb")
(corefoundation)
(contacts-client (param "HOME") (param "TMPDIR"))

;;; 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.facetimemessagestored"))

;; 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.FaceTime")
    (home-subpath "/Library/Caches/CloudKit/com.apple.facetimemessagestored/")
)

;; 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.photos.service")
    (global-name "com.apple.duetactivityscheduler")
    (global-name "com.apple.linkd.application-service")
)

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

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

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

; 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"))
            (home-subpath "/Library/Caches/CloudKit/com.apple.facetimemessagestored/")
            (extension "com.apple.sandbox.application-group")
        )
    )
)


(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")
)

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