;;; Copyright (c) 2021 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 all usernotificationsd mach ports
(allow mach-register
    (global-name "com.apple.usernotifications.listener")
    (global-name "com.apple.usernotifications.tool")
    (global-name "com.apple.usernotificationsvendor.listener")
    (global-name "com.apple.usernotifications.alert-coordination")
    (global-name "com.apple.usernotifications.settings-vendor"))

(allow mach-lookup
    (global-name-prefix "com.apple.usernotifications.")
    (global-name "com.apple.containermanagerd")
    (global-name "com.apple.coreservices.launchservicesd")
    (global-name "com.apple.coreservices.quarantine-resolver")
    (global-name "com.apple.identityservicesd.desktop.auth")
    (global-name "com.apple.spotlight.SearchAgent")
    (global-name "com.apple.spotlight.IndexAgent")
    (global-name "com.apple.replicator.replication")
    (global-name "com.apple.replicatorservices")
    (global-name "com.apple.sharingd.nsxpc")
    (global-name "com.apple.SharingServices")
    (global-name "com.apple.SystemConfiguration.configd")
    (global-name "com.apple.duetactivityscheduler")
    (global-name "com.apple.lsd.mapdb"))    ;; For LSPlugInKitProxy.pluginKitProxy(forIdentifier:)

(allow lsopen)

(allow process-info* (target self))

;; Process info lookup
(allow process-info-pidinfo)

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

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

;; Read/write access to a temporary directory.
(allow file-read* file-write*
    (home-subpath "/Library/UserNotifications")
    (subpath (param "TMPDIR"))
    (subpath (param "DARWIN_CACHE_DIR"))
    (subpath (param "DARWIN_USER_DIR"))
    (extension "com.apple.sandbox.application-group")
    (home-subpath "/Library/IdentityServices/files"))

;; Your preference domain
(allow user-preference-read user-preference-write
        (preference-domain "com.apple.usernotificationsd")
        (preference-domain "kCFPreferencesAnyApplication"))

;; for SWWakingTimer
 (allow iokit-open
        (iokit-user-client-class "RootDomainUserClient"))

(allow file-read*
    (literal "/private/var/db/os_eligibility/eligibility.plist"))
