;;; Copyright (c) 2020 Apple Inc.  All Rights reserved.
;;;
;;; Apple System Private Interface and are subject to change at any time and
;;; without notice.
;;;
;;; https://confluence.sd.apple.com/display/OSSEC/How+To+Sandbox+a+Daemon+on+macOS
;;;

(version 1)

;;;; These 3 lines enforce the profile
(deny default)
(deny file-map-executable iokit-get-properties process-info* nvram*)
(deny dynamic-code-generation)

(import "system.sb")
(system-network) ;; Allow access to the network
(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)

;; Read/write cache access
(let ((cache-path-filter (home-subpath "/Library/Caches/com.apple.appleaccountd")))
    (allow file-read* file-write* cache-path-filter)
    (allow file-issue-extension
        (require-all
            (extension-class "com.apple.app-sandbox.read" "com.apple.app-sandbox.read-write")
            cache-path-filter
        )
    )
)

;;;;
;;;; Security.framework section
;;;;

(allow ipc-posix-shm-read-data ipc-posix-shm-write-create ipc-posix-shm-write-data
    (ipc-posix-name "com.apple.AppleDatabaseChanged")
)

;; Generic use of CloudKit - mostly copied from common.sb on embedded platforms
(allow user-preference-read
    (preference-domain "com.apple.CloudKit"))

;; CloudKit caches
(allow file-read* file-write*
    (home-subpath "/Library/Caches/CloudKit")
)


;; Security.framework read-write access
(allow file-read* file-write*
    (subpath "/private/var/db/mds/system")
)

;;;;
;;;; Preferences
;;;;

;; Preferences (read-write)
(allow user-preference-read user-preference-write
    (preference-domain "com.apple.appleaccountd")
)

;; Preferences (read-only)
(allow user-preference-read
    (preference-domain "com.apple.appleaccount")
    (preference-domain "com.apple.security")
)

(allow user-preference-read
    (preference-domain "kCFPreferencesAnyApplication")
)


;;;;
;;;; Filesystem
;;;;

;; Filesystem read-write access
(allow file-read* file-write*
    (literal "/Library/Preferences/com.apple.security.plist")
    (home-subpath "/Library/Application Support/com.apple.appleaccountd")
    (home-subpath "/Library/com.apple.appleaccountd")
    (home-subpath "/Library/Keychains")
    (literal "/private/var/tmp/com.apple.appleaccountd/")
    (subpath (param "DARWIN_CACHE_DIR"))
    (subpath (param "DARWIN_USER_DIR"))
    (subpath (param "TMPDIR"))
    (home-subpath "/Library/Caches/com.apple.AppleAccount")
    (home-subpath "/Library/HTTPStorages")
    (home-subpath "/Library/HTTPStorages/HTTPStorages/com.apple.appleaccountd")
)

;; Filesystem read-only access
(allow file-read*
    (literal "/System/Library/PrivateFrameworks/AppleAccount.framework/Versions/A/")
    (literal "/System/Library/PrivateFrameworks/AppleAccount.framework/Versions/A/appleaccountd")
    (literal "/usr/libexec")
    (literal "/usr/libexec/appleaccountd")
    (literal "/private/var/preferences/com.apple.networkd.plist")
)

;;;;
;;;; Container Manager Support
;;;;

;; Allow container manager sandbox extensions for daemon containers
(allow file-read* file-write*
    (extension "com.apple.sandbox.container"))

;;;;
;;;; Mach services
;;;;

;; Mach services
(allow mach-lookup
    (global-name "com.apple.ak.auth.xpc")
    (global-name "com.apple.ak.anisette.xpc")
    (global-name "com.apple.ak.custodian.xpc")
    (global-name "com.apple.ak.inheritance.xpc")
    (global-name "com.apple.ak.walrus.xpc")
    (global-name "com.apple.accountsd.accountmanager")
    (global-name "com.apple.familycircle.agent")
    (global-name "com.apple.analyticsd")
    (global-name "com.apple.apsd")
    (global-name "com.apple.cfnetwork.cfnetworkagent")
    (global-name "com.apple.cloudd")
    (global-name "com.apple.contactsd")
    (global-name "com.apple.corefollowup.agent")
    (global-name "com.apple.containermanagerd")
    (global-name "com.apple.identityservicesd.desktop.auth")
    (global-name "com.apple.identityservicesd.embedded.auth")
    (global-name "com.apple.identityservicesd.idquery.desktop.auth")
    (global-name "com.apple.identityservicesd.idquery.embedded.auth")
    (global-name "com.apple.securityd.xpc")
    (global-name "com.apple.security.octagon")
    (global-name "com.apple.SystemConfiguration.configd")
    (global-name "com.apple.frontboard.systemappservices")
    (global-name "com.apple.logind")
    (global-name "com.apple.lsd.mapdb")
    (global-name "com.apple.windowserver.active")
    (global-name "com.apple.cdp.daemon")
    (global-name "com.apple.securityd")
    (global-name "com.apple.usernotifications.usernotificationservice")
    (global-name "com.apple.usernotifications.listener")
    (global-name "com.apple.metadata.mds")
    (global-name "com.apple.SecurityServer")
    (global-name "com.apple.ckdiscretionaryd")
    (global-name "com.apple.AppSSO.service-xpc")
    (global-name "com.apple.usymptomsd")
    (global-name "com.apple.system.opendirectoryd.api")
    (global-name "com.apple.usernoted.daemon_client")
    (global-name "com.apple.coreservices.quarantine-resolver")
    (global-name "com.apple.TapToRadarKit.service")
)

;;;;
;;;; IOKit
;;;;

(allow iokit-open
    (iokit-user-client-class "RootDomainUserClient")
    ;; Allow access to the key store
    (iokit-user-client-class "AppleKeyStoreUserClient")
)

(allow iokit-open-user-client
    (iokit-user-client-class "AvpFairPlayUserClient")
)

;;;;
;;;; Network
;;;;
(allow network-outbound)
(allow network-outbound
    ;; to resolve host names
    (literal "/private/var/run/mDNSResponder")
)

;; Opening pref pane
(allow lsopen)

;; For Contacts
(import "contacts.sb")
(contacts-client (param "HOME") (param "TMPDIR"))

;;;;
;;;; Miscellaneous
;;;;
