;;; Copyright (c) 2023 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)

;;; TODO: Change these to deny before finalizing this profile.
;;; (allow (with report) default)
;;; (allow  (with report) file-map-executable process-info* nvram*)
;;; (allow  (with report) dynamic-code-generation)

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

;; Your preference domain
(allow user-preference-read user-preference-write
       (preference-domain "com.apple.EnergyKit")
       (preference-domain "com.apple.Home")
       (preference-domain "com.apple.wpc.homeservices.energyservices")
       (preference-domain "com.apple.wpc.energyservices.utilityservices")
       (preference-domain "com.apple.wpc.energyservices.gridservices")
       (preference-domain "com.apple.homeenergyd")
       (preference-domain "kCFPreferencesAnyApplication"))

;; Read/write access to a temporary directory.
(allow file-read* file-write*
       (subpath (param "TMPDIR"))
       (subpath (param "DARWIN_CACHE_DIR")))

;; Read/write cache access
(let ((cache-path-filter (home-subpath "/Library/Caches/com.apple.homeenergyd")))
  (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)))

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

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

(let ((homeservices2-cache-path-filter (home-subpath "/Library/Caches/com.apple.wpc.energyservices.gridservices")))
  (allow file-read* file-write* homeservices2-cache-path-filter)
  (allow file-issue-extension
    (require-all
      (extension-class "com.apple.app-sandbox.read" "com.apple.app-sandbox.read-write")
      homeservices2-cache-path-filter)))

(allow mach-lookup (global-name "com.apple.homed.xpc")
                   (global-name "com.apple.homeenergyd.xpc")
                   (global-name "com.apple.homekitevents.xpc")
                   (global-name "com.apple.containermanagerd")
                   (global-name "com.apple.securityd.xpc")
                   (global-name "com.apple.windowserver.active")
                   (global-name "com.apple.weatherkit.authservice")
                   (global-name "com.apple.energykit.localStore")
                   (global-name "com.apple.energykit.sharedStore")
                   (global-name "com.apple.energykit.privateStore")
                   (global-name "com.apple.energykit.store")
                   (global-name "com.apple.lsd.mapdb")
                   (global-name "com.apple.locationd.desktop.synchronous")
                   (global-name "com.apple.tccd")
                   (global-name "com.apple.tccd.system")
                   (global-name "com.apple.CoreLocation.agent")
                   (global-name "com.apple.locationd.desktop.registration")
                   (global-name "com.apple.dnssd.service"))

(allow file-read-data (literal "/private/var/db/nsurlstoraged/dafsaData.bin")
                      (literal "/Library/Preferences/com.apple.networkd.plist")
                      (literal "/private/var/mobile/Library/Weather/"))

(allow file-read*
    (home-subpath "/Library/Weather/"))

(allow file-read* file-write*
    (home-subpath "/Library/HTTPStorages/com.apple.EnergyKitService")
    (home-subpath "/Library/Caches/EnergyKit/")
    (home-subpath "/Library/Caches/com.apple.EnergyKitService/")
    (home-subpath "/Library/Caches/com.apple.wpc.energyservices.gridservices/")
    (extension "com.apple.sandbox.application-group"))

(with-filter (extension "com.apple.app-sandbox.read")
  (allow file-read*)
  (allow file-issue-extension (extension-class "com.apple.app-sandbox.read")))

(with-filter (extension "com.apple.app-sandbox.read-write")
  (allow file-read* file-write*)
  (allow file-issue-extension (extension-class "com.apple.app-sandbox.read" "com.apple.app-sandbox.read-write")))

(allow network-outbound)

(allow system-socket (require-all (socket-domain AF_SYSTEM) (socket-protocol 2)))

