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

;; Open urls such as from notifications actions
(allow lsopen)

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

;; To resolve bundles of clients
(allow process-info-pidinfo)

(system-network)
(allow network-outbound)

;; For access to the system microphone
(allow device-microphone)

(allow user-preference-read
       (preference-domain "com.apple.itunesstored")
       (preference-domain "com.apple.CloudKit")
       (preference-domain "com.apple.itunescloud")
       (preference-domain "com.apple.appleaccount"))
       
(allow user-preference-read user-preference-write
       (preference-domain "com.apple.shazamd")
       (preference-domain "com.apple.ShazamKit")
       (preference-domain "kCFPreferencesAnyApplication")
       (preference-domain "com.apple.AppleMediaServices"))

;; Read/write access to a temporary directory.
(allow file-read* file-write*
       (subpath (param "TMPDIR"))
       (subpath (param "DARWIN_CACHE_DIR"))
       (home-subpath "/Library/Application Support/com.apple.shazamd")
       (home-subpath "/Library/HTTPStorages/com.apple.shazamd"))

;; Read/write cache access ~/Library/Caches/com.apple.shazamd
(let ((cache-path-filter (home-subpath "/Library/Caches/com.apple.shazamd")))
  (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 ((cache-path-filter (home-subpath "/Library/Caches/com.apple.AppleMediaServices")))
  (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)))
      
;; Read/write access to local Shazam CK state cache
(allow file-read* file-write*
      (home-subpath "/Library/Caches/com.apple.shazam.secure")
      (home-subpath "/Library/Caches/com.apple.shazamlibrary.cloud"))

;; Mach lookup, to talk to other processes
(allow mach-lookup
       (global-name "com.apple.ProgressReporting")
       (global-name "com.apple.tccd")
       (global-name "com.apple.tccd.system")
       (global-name "com.apple.accountsd.accountmanager")
       (global-name "com.apple.apsd")
       (global-name "com.apple.audio.AudioQueueServer")
       (global-name "com.apple.fairplayd.versioned")
       (global-name "com.apple.lsd.mapdb")
       (global-name "com.apple.mediasafetynet.exceptions")
       (global-name "com.apple.powerlog.plxpclogger.xpc")
       (global-name "com.apple.systemstatus.activityattribution")
       (global-name "com.apple.usernotifications.usernotificationservice")
       (global-name "com.apple.usernotifications.listener")
       (global-name "com.apple.SystemConfiguration.configd")
       (global-name "com.apple.adid")
       (global-name "com.apple.SystemConfiguration.DNSConfiguration")
       (global-name "com.apple.securityd.xpc")
       (global-name "com.apple.windowserver.active")
       (global-name "com.apple.cloudd")
       (global-name "com.apple.audio.audiohald")
       (global-name "com.apple.audio.AudioComponentRegistrar")
       (global-name "com.apple.coreservices.quarantine-resolver")
       (global-name "com.apple.fpsd")
       (global-name "com.apple.fairplayd")
       (global-name "com.apple.fairplayd.xpc")
       (global-name "com.apple.duetactivityscheduler")
       (global-name "com.apple.xpc.amsaccountsd"))

;; Registering our daemon for push
(allow mach-register
       (global-name "com.apple.aps.shazamd"))

;; For interfacing with the audio driver
(allow file-map-executable
       (subpath "/System/Library/Extensions/AppleHDA.kext"))
(allow iokit-open
        (iokit-user-client-class "IOAudioControlUserClient")
        (iokit-user-client-class "IOAudioEngineUserClient"))

;; For sending data to and from the audio server
(allow ipc-posix-shm-read-data
       ipc-posix-shm-write-data
       (ipc-posix-name-regex #"^AudioIO"))
       
;; For access to the core audio component for recording
(allow file-map-executable
       (subpath "/System/Library/Components/CoreAudio.component/"))
