├── .gitignore ├── LICENSE ├── README.md └── pkgcheck.sh /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | build/ 3 | SamplePkgs/ 4 | scratch/ 5 | *.pkg 6 | *.mpkg 7 | 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Check Installer Pkgs for deprecated scripts 2 | 3 | macOS 10.15 Catalina [deprecated the built-in `/bin/bash`](https://support.apple.com/en-us/HT208050). [I have talked about this at length](https://scriptingosx.com/2019/06/moving-to-zsh/). 4 | 5 | The [release notes for Catalina](https://developer.apple.com/documentation/macos_release_notes/macos_catalina_10_15_beta_6_release_notes) also tell us that other built-in scripting runtimes, namely Python, Perl, and Ruby. Will not be included in future macOS releases (post-Catalina) any more. 6 | 7 | [Starting with macOS 12.3](https://developer.apple.com/documentation/macos-release-notes/macos-12_3-release-notes), the Python 2.7 binary `/usr/bin/python` will _not_ work anymore. Calls to `python` will fail. 8 | 9 | This means, that if you want to use bash, Python, Perl, or Ruby on macOS, you will have to install, and maintain your own version in the future. 10 | 11 | However, scripts in installation packages, _cannot_ rely on any of these interpreters being available in future, post-Catalina versions of macOS. Installer pkgs can be run in all kinds of environments and at all times, and you would not want them to fail, because a dependency is missing. 12 | 13 | If you are building installation scripts, you need to check if any of the installation scripts use one of these interpreters and fix them. 14 | 15 | > I recommend to use `/bin/sh` for installation scripts, since that will run in _any_ macOS context, even the Recovery system and a 'future' macOS that may not have `/bin/bash` 16 | 17 | If you are using third-party installer packages, you may also want to check them for these interpreters, and notify the developer that these packages will break in future versions of macOS. 18 | 19 | To check a flat installer package, you would expand it with `pkgutil --expand` and then look at script files in the `Scripts` folder. This will work fine for a package or two, but gets tedious really quickly, especially with large distribution pkgs with many components (e.g. Office). 20 | 21 | So... I wrote a script to do it. The script should handle normal component pkgs, distribution pkgs and the legacy bundle pkgs and mpkgs. It will also 22 | 23 | ## What the script does 24 | 25 | Once I had written the code to inspect all these types of pkgs, I realized I could grab all _other_ kinds of information, as well. The `pkgcheck.sh` script will check for: 26 | 27 | - Signature _and_ Notarization 28 | - Type of Package: Component, Distribution, legacy bundle or mpkg 29 | - Identifier and version (when present) 30 | - Install-location 31 | - for Distribution and mpkg types, shows the information for all components as well 32 | - for every script in a pkg or component, checks the first line of the script for shebangs of the deprecated interpreters (`/bin/bash`, `/usr/bin/python`, `/usr/bin/perl`, and `/usr/bin/ruby`, also using these binaries with `/usr/bin/env`) and print a warning when found 33 | - `/usr/bin/python` and `/usr/bin/env python` will show an error 34 | - if the script contains the string `python` (that is not in a comment line) it will also show an error, this might generate some false positives (for example, calls to `python3` will also trigger this, but they would probably be problematic, as well) 35 | 36 | ## How to run pkgcheck.sh 37 | 38 | Run the script with the target pkg file as an argument: 39 | 40 | ``` 41 | % ./pkgcheck.sh sample.pkg 42 | ``` 43 | 44 | You can give more than one file: 45 | 46 | ``` 47 | % ./pkgcheck.sh file1.pkg file2.pkg ... 48 | ``` 49 | 50 | When you pass a directory, `pkgcheck.sh` will _recursively_ search for all files or bundle directories with the `pkg` or `mpkg` extension in that directory: 51 | 52 | ``` 53 | % ./pkgcheck.sh SamplePkgs 54 | ``` 55 | 56 | ## Features and Errors 57 | 58 | There are a few more things that I think might be useful to check in this script. Most of all, I want to add an indicator whether a component is enabled by default or not. If you can think of any other valuable data to display, let me know. (Issue or Pull Request or just ping me on MacAdmins Slack) 59 | 60 | I have tested the script against many pkgs that I came across. However, there are likely edge cases that I haven't anticipated, which might break the script. If you run into any of those, let me know. (File an Issue or Pull Request.) Having the troublesome pkg would of course be a great help. 61 | 62 | Note: the script will create a `scratch` directory for temporary file extractions. The script doesn't actually expand the entire pkg file, only the `Scripts` sub-archive. The `scratch` folder will be cleaned out at the beginning of the next run, but not when the script ends, as you might want to do some further inspections. 63 | 64 | ## Sample outputs 65 | 66 | This is a sample pkg I build in my book, it has pre- and postinstall scripts using a `/bin/bash` shebang: 67 | 68 | ``` 69 | % ./pkgcheck.sh SourceCodePro-2.030d.pkg 70 | SourceCodePro-2.030d 71 | SamplePkgs/SourceCodePro-2.030d.pkg 72 | Signature: None 73 | Notarized: No 74 | Type: Flat Component PKG 75 | Identifier: com.example.SourceCodePro 76 | Version: 2.030d 77 | Location: / 78 | Contains 2 resource files 79 | postinstall has shebang #!/bin/bash 80 | preinstall has shebang #!/bin/bash 81 | ``` 82 | 83 | This is the experimental _notarized_ pkg installer for [`desktoppr`](https://github.com/scriptingosx/desktoppr): 84 | 85 | ``` 86 | % ./pkgcheck.sh desktoppr-0.2.pkg 87 | desktoppr-0.2 88 | SamplePkgs/desktoppr-0.2.pkg 89 | Signature: Developer ID Installer: Armin Briegel (JME5BW3F3R) 90 | Notarized: Yes 91 | Type: Flat Component PKG 92 | Identifier: com.scriptingosx.desktoppr 93 | Version: 0.2 94 | Contains 0 resource files 95 | ``` 96 | 97 | And finally, this is a big one, the Microsoft Office installer: (they have some work to do to clean up those scripts) 98 | 99 | ``` 100 | % ./pkgcheck.sh Microsoft\ Office\ 16.27.19071500_Installer.pkg 101 | Microsoft Office 16.27.19071500_Installer 102 | SamplePkgs/Microsoft Office 16.27.19071500_Installer.pkg 103 | Signature: Developer ID Installer: Microsoft Corporation (UBF8T346G9) 104 | Notarized: No 105 | Type: Flat Distribution PKG 106 | Contains 11 component pkgs 107 | 108 | Microsoft_Word_Internal 109 | Type: Flat Component PKG 110 | Identifier: com.microsoft.package.Microsoft_Word.app 111 | Version: 16.27.19071500 112 | Location: /Applications 113 | Contains 3 resource files 114 | 115 | Microsoft_Excel_Internal 116 | Type: Flat Component PKG 117 | Identifier: com.microsoft.package.Microsoft_Excel.app 118 | Version: 16.27.19071500 119 | Location: /Applications 120 | Contains 2 resource files 121 | 122 | Microsoft_PowerPoint_Internal 123 | Type: Flat Component PKG 124 | Identifier: com.microsoft.package.Microsoft_PowerPoint.app 125 | Version: 16.27.19071500 126 | Location: /Applications 127 | Contains 2 resource files 128 | 129 | Microsoft_OneNote_Internal 130 | Type: Flat Component PKG 131 | Identifier: com.microsoft.package.Microsoft_OneNote.app 132 | Version: 16.27.19071500 133 | Location: /Applications 134 | Contains 2 resource files 135 | 136 | Microsoft_Outlook_Internal 137 | Type: Flat Component PKG 138 | Identifier: com.microsoft.package.Microsoft_Outlook.app 139 | Version: 16.27.19071500 140 | Location: /Applications 141 | Contains 2 resource files 142 | 143 | OneDrive 144 | Type: Flat Component PKG 145 | Identifier: com.microsoft.OneDrive 146 | Version: 19.70.410 147 | Location: /Applications 148 | Contains 30 resource files 149 | postinstall has shebang #!/bin/bash 150 | od_logging has shebang #!/bin/bash 151 | od_service has shebang #!/bin/bash 152 | od_migration has shebang #!/bin/bash 153 | preinstall has shebang #!/bin/bash 154 | 155 | Office16_all_autoupdate 156 | Type: Flat Component PKG 157 | Identifier: com.microsoft.package.Microsoft_AutoUpdate.app 158 | Version: 4.13.19071500 159 | Location: /Library/Application Support/Microsoft/MAU2.0 160 | Contains 2 resource files 161 | postinstall has shebang #!/bin/bash 162 | preinstall has shebang #!/bin/bash 163 | 164 | Office16_all_licensing 165 | Type: Flat Component PKG 166 | Identifier: com.microsoft.pkg.licensing 167 | Version: 16.27.19071500 168 | Location: / 169 | Contains 2 resource files 170 | dockutil has shebang #!/usr/bin/python 171 | 172 | Office_fonts 173 | Type: Flat Component PKG 174 | Identifier: com.microsoft.package.DFonts 175 | Version: 0 176 | Location: /private/tmp/com.microsoft.package.DFonts 177 | Contains 1 resource files 178 | postinstall has shebang #!/bin/bash 179 | 180 | Office_frameworks 181 | Type: Flat Component PKG 182 | Identifier: com.microsoft.package.Frameworks 183 | Version: 0 184 | Location: /private/tmp/com.microsoft.package.Frameworks 185 | Contains 1 resource files 186 | postinstall has shebang #!/bin/bash 187 | 188 | Office_proofing 189 | Type: Flat Component PKG 190 | Identifier: com.microsoft.package.Proofing_Tools 191 | Version: 0 192 | Location: /private/tmp/com.microsoft.package.Proofing_Tools 193 | Contains 1 resource files 194 | postinstall has shebang #!/bin/bash 195 | 196 | ``` 197 | 198 | 199 | -------------------------------------------------------------------------------- /pkgcheck.sh: -------------------------------------------------------------------------------- 1 | #!/bin/zsh 2 | 3 | # pkgcheck.sh 4 | 5 | # 2019 - Armin Briegel - Scripting OS X 6 | 7 | # this script will look into pkg files and warn if any of the installation scripts 8 | # use one of the following shebangs: 9 | # 10 | # /bin/bash 11 | # /usr/bin/python 12 | # /usr/bin/perl 13 | # /usr/bin/ruby 14 | # 15 | # also checks for signatures and notarization and other information 16 | 17 | 18 | function mkcleandir() { # $1: dirpath 19 | local dirpath=${1:?"no dir path"} 20 | if [[ -d $dirpath ]]; then 21 | if ! rm -rf "${dirpath:?no dir path}"; then 22 | return 1 23 | fi 24 | fi 25 | if ! mkdir -p "$dirpath"; then 26 | return 2 27 | fi 28 | } 29 | 30 | function getPkgSignature() { # $1: pkgpath 31 | local pkgpath=${1:?"no pkg path"} 32 | signature=$(pkgutil --check-signature "$pkgpath" | fgrep '1. ' | cut -c 8- ) 33 | if [[ -z $signature ]]; then 34 | signature="$fg[yellow]None$reset_color" 35 | fi 36 | echo "$fg[green]$signature$reset_color" 37 | return 38 | } 39 | 40 | function getPkgNotarized() { # $1: pkgpath 41 | local pkgpath=${1:?"no pkg path"} 42 | 43 | if notary_result=$(spctl --assess -vvv --type install $pkgpath 2>&1); then 44 | notary_source=$(echo "$notary_result" | awk -F '=' '/source/ { print $2 }') 45 | echo "$fg[green]Yes, $notary_source$reset_color" 46 | else 47 | echo "$fg[yellow]No$reset_color" 48 | fi 49 | } 50 | 51 | function getInfoPlistValueForKey() { # $1: pkgpath $2: key 52 | local pkgpath=${1:?"no pkg path"} 53 | local key=${2:?"no key"} 54 | 55 | infoplist="$pkgpath/Contents/Info.plist" 56 | if [[ -r "$infoplist" ]]; then 57 | /usr/libexec/PlistBuddy -c "print $key" "$infoplist" 58 | fi 59 | return 60 | } 61 | 62 | function checkFilesInDir() { # $1: dirpath $2: level 63 | local dirpath=${1:?"no directory path"} 64 | 65 | local level=${2:-0} 66 | if [[ level -gt 0 ]]; then 67 | indent=" " 68 | else 69 | indent="" 70 | fi 71 | 72 | local foundscripts=$(find "$dirpath" -type f -print0 ) 73 | local scriptfiles=( ${(0)foundscripts} ) 74 | local scripts_count=${#scriptfiles} 75 | 76 | echo "${indent}Contains $scripts_count resource files" 77 | 78 | for f in "${scriptfiles[@]}"; do 79 | if [[ -e "$f" ]]; then 80 | relpath="${f#"$dirpath/"}" 81 | 82 | file_description="$(file -b "$f")" 83 | #echo "$indent$file_description" 84 | if [[ "$file_description" == *"script text executable"* ]]; then 85 | 86 | # check for deprecated shebangs 87 | shebang=$(head -n 1 "$f" | tr -d $'\n') 88 | lastelement=${shebang##*/} 89 | if [[ $shebang == "#!/bin/bash" || \ 90 | $shebang == "#!/usr/bin/env bash" || \ 91 | $shebang == "#!/usr/bin/ruby" || \ 92 | $shebang == "#!/usr/bin/env ruby" || \ 93 | $shebang == "#!/usr/bin/perl" || \ 94 | $shebang == "#!/usr/bin/env perl" ]]; then 95 | echo "$indent$fg[yellow]$relpath has shebang $shebang$reset_color" 96 | fi 97 | 98 | # python gets extra treatment since it will break in macOS 12.3+ 99 | if [[ $shebang == "#!/usr/bin/python" || \ 100 | $shebang == "#!/usr/bin/env python" ]]; then 101 | echo "$indent$fg[red]$relpath has shebang $shebang$reset_color" 102 | fi 103 | 104 | # check for uses of 'python' in code 105 | if grep --invert-match '^#' "$f" | grep --quiet 'python'; then 106 | echo "$indent$fg[red]$relpath calls 'python' in code$reset_color" 107 | fi 108 | fi 109 | fi 110 | done 111 | } 112 | 113 | function checkBundlePKG() { # $1: pkgpath $2: level 114 | local pkgpath=${1:?"no pkg path"} 115 | local pkgfullname=$(basename $pkgpath) 116 | local pkgname=${pkgfullname%.*} # remove extension 117 | 118 | local level=${2:-0} 119 | if [[ level -gt 0 ]]; then 120 | indent=" " 121 | echo $indent$bold_color$pkgname$reset_color 122 | #echo $indent$pkgpath 123 | else 124 | indent="" 125 | fi 126 | 127 | echo $indent"Type: PKG Bundle" 128 | 129 | # get version and identifier 130 | 131 | pkgidentifier=$(getInfoPlistValueForKey "$pkgpath" "CFBundleIdentifier") 132 | if [[ -n $pkgidentifier ]]; then 133 | echo $indent"Identifier: $pkgidentifier" 134 | fi 135 | 136 | pkgversion=$(getInfoPlistValueForKey "$pkgpath" "CFBundleShortVersionString") 137 | if [[ -n $pkgversion ]]; then 138 | echo $indent"Version: $pkgversion" 139 | fi 140 | 141 | pkglocation=$(getInfoPlistValueForKey "$pkgpath" "IFPkgFlagDefaultLocation") 142 | if [[ -n $pkglocation ]]; then 143 | echo $indent"Location: $pkglocation" 144 | fi 145 | 146 | # check files resources folder 147 | resourcesfolder="$pkgpath/Contents/Resources" 148 | if [[ -d $resourcesfolder ]]; then 149 | checkFilesInDir "$resourcesfolder" "$level" 150 | fi 151 | 152 | echo 153 | } 154 | 155 | function checkBundleMPKG() { # $1: pkgpath 156 | local pkgpath=${1:?"no pkg path"} 157 | local pkgfullname=$(basename $pkgpath) 158 | local pkgname=${pkgfullname%.*} # remove extension 159 | 160 | echo "Type: MPKG Bundle" 161 | 162 | IFS=$'\n' 163 | components=( $(find "$pkgpath" -iname '*.pkg') ) 164 | components_count=${#components} 165 | echo "Contains $components_count component pkgs" 166 | echo 167 | 168 | for component in "${components[@]}"; do 169 | checkBundlePKG "$component" 1 170 | done 171 | } 172 | 173 | function checkComponentPKG() { # $1: pkgpath $2: level 174 | local pkgpath=${1:?"no pkg path"} 175 | local pkgfullname=$(basename $pkgpath) 176 | local pkgname=${pkgfullname%.*} # remove extension 177 | 178 | local level=${2:-0} 179 | if [[ level -gt 0 ]]; then 180 | indent=" " 181 | echo 182 | echo $indent$bold_color$pkgname$reset_color 183 | echo $indent$pkgpath 184 | else 185 | indent="" 186 | fi 187 | 188 | echo $indent"Type: Flat Component PKG" 189 | 190 | # expand the flat pkg 191 | local pkgdir="$scratchdir/$pkgname" 192 | 193 | if [[ -d "$pkgdir" ]] ; then 194 | rm -r "$pkgdir" || return 1 195 | fi 196 | pkgutil --expand "$pkgpath" "$pkgdir" 197 | 198 | # determine identifier and version, if present 199 | pkginfo="$pkgdir/PackageInfo" 200 | if [[ -f "$pkginfo" ]]; then 201 | # try to extract identifier 202 | pkgidentifier=$(xmllint --xpath "string(//pkg-info/@identifier)" ${pkginfo}) 203 | if [[ -n $pkgidentifier ]]; then 204 | echo "Identifier: $pkgidentifier" 205 | fi 206 | pkgversion=$(xmllint --xpath "string(//pkg-info/@version)" ${pkginfo}) 207 | if [[ -n $pkgversion ]]; then 208 | echo "Version: $pkgversion" 209 | fi 210 | pkglocation=$(xmllint --xpath "string(//pkg-info/@install-location)" ${pkginfo}) 211 | if [[ -n $pkglocation ]]; then 212 | echo "Location: $pkglocation" 213 | fi 214 | 215 | fi 216 | 217 | # does the pkg have a Scripts dir? 218 | if [[ -d "$pkgdir/Scripts" ]] ; then 219 | checkFilesInDir "$pkgdir/Scripts" "$level" 220 | fi 221 | 222 | echo 223 | # clean up 224 | rm -rf "$pkgdir" 225 | } 226 | 227 | function checkDistributionPKG() { # $1: pkgpath 228 | local pkgpath=${1:?"no pkg path"} 229 | local pkgfullname=$(basename $pkgpath) 230 | local pkgname=${pkgfullname%.*} # remove extension 231 | 232 | echo "Type: Flat Distribution PKG" 233 | 234 | # expand the flat pkg 235 | local pkgdir="$scratchdir/$pkgname" 236 | 237 | if [[ -d "$pkgdir" ]] ; then 238 | rm -r "$pkgdir" || return 1 239 | fi 240 | pkgutil --expand "$pkgpath" "$pkgdir" 241 | 242 | # determine identifier and version, if present 243 | distributionxml="$pkgdir/Distribution" 244 | if [[ -f "$distributionxml" ]]; then 245 | # distribution pkg, try to extract identifier 246 | pkgidentifier=$(xmllint --xpath "string(//installer-gui-script/product/@id)" ${distributionxml}) 247 | if [[ -n $pkgidentifier ]]; then 248 | echo "Identifier: $pkgidentifier" 249 | fi 250 | pkgversion=$(xmllint --xpath "string(//installer-gui-script/product/@version)" ${distributionxml}) 251 | if [[ -n $pkgversion ]]; then 252 | echo "Version: $pkgversion" 253 | fi 254 | fi 255 | 256 | # find component pkgs 257 | IFS=$'\n' 258 | components=($(ls -d1 "$pkgdir"/*.pkg)) 259 | components_count=${#components} 260 | echo "Contains ${#components} component pkgs" 261 | echo 262 | 263 | if [[ $components_count -gt 0 ]]; then 264 | for c in $components ; do 265 | echo " $bold_color$cname$reset_color" 266 | echo " Type: Flat Component PKG" 267 | 268 | # determine identifier and version, if present 269 | pkginfo="$c/PackageInfo" 270 | if [[ -f "$pkginfo" ]]; then 271 | # try to extract identifier 272 | pkgidentifier=$(xmllint --xpath "string(//pkg-info/@identifier)" "${pkginfo}") 273 | if [[ -n $pkgidentifier ]]; then 274 | echo " Identifier: $pkgidentifier" 275 | fi 276 | pkgversion=$(xmllint --xpath "string(//pkg-info/@version)" "${pkginfo}") 277 | if [[ -n $pkgversion ]]; then 278 | echo " Version: $pkgversion" 279 | fi 280 | pkglocation=$(xmllint --xpath "string(//pkg-info/@install-location)" "${pkginfo}") 281 | if [[ -n $pkglocation ]]; then 282 | echo " Location: $pkglocation" 283 | fi 284 | fi 285 | 286 | 287 | # does the pkg have a Scripts directory? 288 | if [[ -d "$c/Scripts" ]] ; then 289 | checkFilesInDir "$c/Scripts" 1 290 | fi 291 | echo 292 | done 293 | fi 294 | # clean up 295 | rm -rf "$pkgdir" 296 | } 297 | 298 | function checkPkg() { # $1: pkgpath 299 | local pkgpath=${1:?"no pkg path"} 300 | local pkgfullname=$(basename $pkgpath) 301 | local pkgname=${pkgfullname%.*} # remove extension 302 | 303 | type="" 304 | 305 | # if extension is not pkg or mpkg: no pkg installer 306 | if [[ $pkgpath != *.(pkg|mpkg) ]]; then 307 | type="no_pkg" 308 | echo "$pkgname has no pkg or mpkg file extension" 309 | return 1 310 | fi 311 | 312 | echo $bold_color$pkgname$reset_color 313 | echo $pkgpath 314 | echo "Signature: "$(getPkgSignature "$pkgpath") 315 | if [[ $devtools == "installed" ]]; then 316 | echo "Notarized: "$(getPkgNotarized "$pkgpath") 317 | fi 318 | 319 | # mpkg extension : mpkg bundle type 320 | if [[ $pkgpath == *.mpkg ]]; then 321 | checkBundleMPKG "$pkgpath" 322 | return 0 323 | elif [[ -d $pkgpath ]]; then 324 | checkBundlePKG "$pkgpath" 325 | else 326 | # flat pkg, look for Distribution 327 | distribution=$(xar -tf "$pkgpath" | grep Distribution 2>/dev/null ) 328 | if [[ $? == 0 ]]; then 329 | checkDistributionPKG "$pkgpath" 330 | else 331 | # no distribution xml, likely a component pkg 332 | checkComponentPKG "$pkgpath" 333 | fi 334 | fi 335 | 336 | } 337 | 338 | function checkDmg() { # $1: dmgpath 339 | local dmgpath=${1:?"no dmg path"} 340 | 341 | if [[ ! -f $dmgpath ]]; then 342 | return 1 343 | fi 344 | 345 | # mount dmg 346 | # piping in 'Y' on stdin to auto-approve license agreements 347 | dmg_volume_path=$(echo 'Y' | hdiutil attach "$dmgpath" -noverify -nobrowse -readonly | tail -n 1 | cut -c 54- ) 348 | 349 | echo "$fg[blue]Mounted $dmgpath to $dmg_volume_path${reset_color}" 350 | echo 351 | 352 | # check dmg 353 | checkDirectory "$dmg_volume_path" 354 | 355 | # unmount dmg 356 | if hdiutil detach "$dmg_volume_path" >/dev/null ; then 357 | echo "$fg[blue]unmounted $dmg_volume_path ($dmgpath)$reset_color" 358 | else 359 | echo "$fg[red]could not unmount $dmg_volume_path ($dmgpath)$reset_color" 360 | fi 361 | echo 362 | } 363 | 364 | function checkDirectory() { # $1: dirpath 365 | local dirpath=${1:?"no directory path"} 366 | 367 | if [[ ! -d $dirpath ]]; then 368 | return 1 369 | fi 370 | 371 | local foundpkgs=$(find "$dirpath" -not \( -ipath '*.mpkg/*' -or -iname '._*' \) -and \( -iname '*.pkg' -or -iname '*.mpkg' \) -print0 ) 372 | local pkglist=( ${(0)foundpkgs} ) 373 | # find all pkg and mpkgs in the directory, excluding component pkgs in mpkgs 374 | for x in $pkglist ; do 375 | checkPkg "$x" 376 | done 377 | 378 | local founddmgs=$(find "$dirpath" -iname '*.dmg' -print0 ) 379 | local dmglist=( ${(0)founddmgs} ) 380 | # find all the dmgs in the directory 381 | for x in $dmglist; do 382 | checkDmg "$x" 383 | done 384 | } 385 | 386 | # reset zsh 387 | emulate -LR zsh 388 | 389 | # set -x 390 | 391 | # load colors for nicer output 392 | autoload -U colors && colors 393 | 394 | # are the dev tools installed (this is required for the stapler tool) 395 | if xcode-select -p >/dev/null; then 396 | devtools="installed" 397 | else 398 | devtools="none" 399 | fi 400 | 401 | # this script's dir: 402 | scriptdir=$(dirname $0) 403 | 404 | # scratch space 405 | scratchdir="$scriptdir/scratch/" 406 | if ! mkcleandir "$scratchdir"; then 407 | echo "couldn't clean $scratchdir" 408 | exit 1 409 | fi 410 | 411 | for arg in "$@"; do 412 | arg_ext="${arg##*.}" 413 | if [[ $arg_ext == "pkg" || $arg_ext == "mpkg" ]]; then 414 | checkPkg "$arg" 415 | elif [[ $arg_ext == "dmg" ]]; then 416 | checkDmg "$arg" 417 | elif [[ -d $arg ]]; then 418 | checkDirectory "$arg" 419 | else 420 | echo 421 | echo "$fg[red]pkgcheck: cannot process $arg$reset_color" 422 | echo 423 | fi 424 | done 425 | 426 | exit 0 427 | 428 | # todo 429 | # √ check if pkg is signed 430 | # √ check if pkg is notarized 431 | # √ get pkg version when available 432 | # √ get pkg identifier when available 433 | # √ when arg 1 ends in pkg or mpkg use that as the only target 434 | # - show if components are enabled or disabled 435 | # - clean up code to work on flat components inside a distribution pkg 436 | # √ show install location 437 | # - mount dmg files and inspect pkgs inside 438 | 439 | 440 | --------------------------------------------------------------------------------