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

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

;; Read/write access to temporary location used by atomic file saves.
(allow file-read* file-write*
       (mount-relative-regex #"^/\.TemporaryItems(/|$)"))

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

;;; bookdatastored specific rules

(allow distributed-notification-post)

(allow file-read*
    (home-literal "/Library/Preferences/com.apple.security.plist")
    (regex #"/private/var/db/mds/messages/.*/se_SecurityMessages"))

(allow file-read* file-write*
    (home-subpath "/Library/Caches/CloudKit/com.apple.iBooks.BookDataStoreService/"))

(allow file-read* file-write*
    (home-subpath "/Library/Group Containers/group.com.apple.iBooks/"))

(allow mach-lookup
    (global-name "com.apple.accountsd.accountmanager")
    (global-name "com.apple.adid")
    (global-name "com.apple.ak.anisette.xpc")
    (global-name "com.apple.apsd")
    (global-name "com.apple.cloudd")
    (global-name "com.apple.containermanagerd")
    (global-name "com.apple.DiskArbitration.diskarbitrationd")
    (global-name "com.apple.duetactivityscheduler")
    (global-name "com.apple.FileCoordination")
    (global-name "com.apple.kvsd")
    (global-name "com.apple.lsd.mapdb")
    (global-name "com.apple.SystemConfiguration.DNSConfiguration")
    (global-name "com.apple.tccd")
    (global-name "com.apple.windowserver.active")
    (global-name "com.apple.xpc.amstoold")
    (global-name "com.apple.xpc.amsaccountsd")
)

(allow user-preference-read
    (preference-domain "com.apple.purplebuddy")
    (preference-domain "com.apple.CloudKit")
    (preference-domain "com.apple.security")
    (preference-domain "kCFPreferencesAnyApplication"))

(allow user-preference-read user-preference-write
    (preference-domain "bookdatastored"))

(allow file-issue-extension
    (regex #"/Library/Caches/com\.apple\.AppleMediaServices"))
    
(allow file-issue-extension
  (require-all
    (extension-class "com.apple.cfprefsd.read")
    (regex #"/Library/Group Containers/group.com.apple.iBooks/Library/Preferences")))
    
(allow file-read* file-write*
    (regex #"/Library/Caches/com\.apple\.AppleMediaServices"))

(allow network-outbound)
(allow system-socket)

(allow user-preference-read user-preference-write
    (preference-domain "com.apple.AppleMediaServices"))

(system-network)
