| 1 |
#!/usr/bin/env bash |
| 2 |
|
| 3 |
# Use of unset variable is an error |
| 4 |
set -u |
| 5 |
# If any part of a pipeline of commands fails, the whole pipeline fails |
| 6 |
set -o pipefail |
| 7 |
|
| 8 |
# Script to sign executables, dylibs and frameworks in an app bundle plus the bundle itself. Called |
| 9 |
# from installer::simplepackage::create_package() in solenv/bin/modules/installer/simplepackage.pm |
| 10 |
# and the test-install target in Makefile.in. |
| 11 |
|
| 12 |
test `uname` = Darwin || { echo This is for macOS only; exit 1; } |
| 13 |
|
| 14 |
test $# = 1 || { echo Usage: $0 app-bundle; exit 1; } |
| 15 |
|
| 16 |
for V in \ |
| 17 |
BUILDDIR \ |
| 18 |
MACOSX_BUNDLE_IDENTIFIER; do |
| 19 |
if test -z "$(eval echo '$'$V)"; then |
| 20 |
echo No '$'$V "environment variable! This should be run in a build only" |
| 21 |
exit 1 |
| 22 |
fi |
| 23 |
done |
| 24 |
|
| 25 |
APP_BUNDLE="$1" |
| 26 |
entitlements= |
| 27 |
entitlements_helper= |
| 28 |
entitlements_quicklookappex="--entitlements $SRCDIR/sysui/desktop/macosx/quicklookappex.entitlements" |
| 29 |
launch_constraint="--launch-constraint-parent $BUILDDIR/sysui/desktop/macosx/LaunchConstraint.plist" |
| 30 |
application_identifier= |
| 31 |
if test -n "$ENABLE_MACOSX_SANDBOX"; then |
| 32 |
# In a sandboxed build executables need the entitlements |
| 33 |
entitlements="--entitlements $BUILDDIR/lo.xcent" |
| 34 |
# helper utilities must be signed with only the sandbox and inherit entitlements |
| 35 |
entitlements_helper="--entitlements $SRCDIR/sysui/desktop/macosx/sandbox_inherit.entitlements" |
| 36 |
application_identifier=`/usr/libexec/PlistBuddy -c "print com.apple.application-identifier" $BUILDDIR/lo.xcent` |
| 37 |
# remove the key from the entitlement - only use it when signing the whole bundle in the final step |
| 38 |
/usr/libexec/PlistBuddy -c "delete com.apple.application-identifier" $BUILDDIR/lo.xcent |
| 39 |
# HACK: remove donate menu entries, need to support apple-pay and be verified |
| 40 |
# as non profit as a bare minimum to allow asking.... |
| 41 |
sed -I "" -e '\#<menu:menuitem menu:id=".uno:Donation"/>#d' $APP_BUNDLE/Contents/Resources/config/soffice.cfg/modules/*/menubar/menubar.xml |
| 42 |
else |
| 43 |
entitlements="--entitlements $BUILDDIR/hardened_runtime.xcent" |
| 44 |
entitlements_helper=$entitlements |
| 45 |
fi |
| 46 |
|
| 47 |
if test -z "$MACOSX_CODESIGNING_IDENTITY"; then |
| 48 |
if test -n "$ENABLE_RELEASE_BUILD"; then |
| 49 |
echo "This is a release build! This should be run in a non-release build only" |
| 50 |
exit 1 |
| 51 |
fi |
| 52 |
|
| 53 |
# Quick Look plugins will only load if with sandbox entitlements |
| 54 |
find "$APP_BUNDLE" -name 'QuickLook*.appex' -type d | |
| 55 |
while read appex; do |
| 56 |
codesign --force --prefix=$MACOSX_BUNDLE_IDENTIFIER. --sign - $entitlements_quicklookappex "$appex" || exit 1 |
| 57 |
done |
| 58 |
|
| 59 |
# Skip codesigning for non-release builds if there is no identity set but |
| 60 |
# set entitlements to allow Xcode's Instruments application to connect to |
| 61 |
# the application. Note: the following command fails on some Mac Intel |
| 62 |
# machines, and since this not a release build, ignore any failures. |
| 63 |
# Related: tdf#159529 fix increasing failures when setting entitlements |
| 64 |
# Starting in one of the Xcode versions 15.2 or earlier, setting the |
| 65 |
# entitlements without a certificate started failing on Mac Silicon. |
| 66 |
# The hacky solution is to make a copy of the application's executable, |
| 67 |
# set the entitlements on that binary only, and then move the copied |
| 68 |
# binary back. |
| 69 |
rm -f "$APP_BUNDLE/Contents/MacOS/soffice.withentitlements" |
| 70 |
cp "$APP_BUNDLE/Contents/MacOS/soffice" "$APP_BUNDLE/Contents/MacOS/soffice.withentitlements" |
| 71 |
if codesign --force --identifier="${MACOSX_BUNDLE_IDENTIFIER}" --sign - $entitlements "$APP_BUNDLE/Contents/MacOS/soffice.withentitlements"; then |
| 72 |
mv "$APP_BUNDLE/Contents/MacOS/soffice.withentitlements" "$APP_BUNDLE/Contents/MacOS/soffice" |
| 73 |
else |
| 74 |
rm "$APP_BUNDLE/Contents/MacOS/soffice.withentitlements" |
| 75 |
fi |
| 76 |
|
| 77 |
exit 0 |
| 78 |
fi |
| 79 |
|
| 80 |
# Sign dylibs |
| 81 |
# |
| 82 |
# The dylibs in the Python framework are called *.so. Go figure |
| 83 |
# |
| 84 |
# Make a depth-first search to sign the contents of e.g. the spotlight plugin |
| 85 |
# before attempting to sign the plugin itself |
| 86 |
|
| 87 |
find "$APP_BUNDLE" \( -name '*.dylib' -or -name '*.dylib.*' -or -name '*.so' -or -name '*.jnilib' \) ! -type l | |
| 88 |
while read file; do |
| 89 |
id=`echo ${file#${APP_BUNDLE}/Contents/} | sed -e 's,/,.,g'` |
| 90 |
codesign --force --identifier=$MACOSX_BUNDLE_IDENTIFIER.$id --sign "$MACOSX_CODESIGNING_IDENTITY" "$file" || exit 1 |
| 91 |
done |
| 92 |
|
| 93 |
# Sign included bundles. First .app ones (i.e. the Python.app inside |
| 94 |
# the LibreOfficePython.framework. Be generic for kicks...) |
| 95 |
|
| 96 |
find "$APP_BUNDLE"/Contents -name '*.app' -type d | |
| 97 |
while read app; do |
| 98 |
# Assume the app has a XML (and not binary) Info.plist |
| 99 |
id=`grep -A 1 '<key>CFBundleIdentifier</key>' "$app/Contents/Info.plist" | tail -1 | sed -e 's,.*<string>,,' -e 's,</string>.*,,'` |
| 100 |
codesign --timestamp --options=runtime --force --identifier=$id --sign "$MACOSX_CODESIGNING_IDENTITY" $entitlements $launch_constraint "$app" || exit 1 |
| 101 |
done |
| 102 |
|
| 103 |
# Then .framework ones. Again, be generic just for kicks. |
| 104 |
|
| 105 |
find "$APP_BUNDLE" -name '*.framework' -type d | |
| 106 |
while read framework; do |
| 107 |
for version in "$framework"/Versions/*; do |
| 108 |
if test ! -L "$version" -a -d "$version"; then |
| 109 |
# Assume the framework has a XML (and not binary) Info.plist |
| 110 |
id=`grep -A 1 '<key>CFBundleIdentifier</key>' $version/Resources/Info.plist | tail -1 | sed -e 's,.*<string>,,' -e 's,</string>.*,,'` |
| 111 |
if test -d $version/bin; then |
| 112 |
# files in bin are not covered by signing the framework... |
| 113 |
for scriptorexecutable in $(find $version/bin/ -type f); do |
| 114 |
codesign --timestamp --options=runtime --force --identifier=$id --sign "$MACOSX_CODESIGNING_IDENTITY" $launch_constraint "$scriptorexecutable" || exit 1 |
| 115 |
done |
| 116 |
fi |
| 117 |
codesign --force --identifier=$id --sign "$MACOSX_CODESIGNING_IDENTITY" $launch_constraint "$version" || exit 1 |
| 118 |
fi |
| 119 |
done |
| 120 |
done |
| 121 |
|
| 122 |
# Then mdimporters |
| 123 |
|
| 124 |
find "$APP_BUNDLE" -name '*.mdimporter' -type d | |
| 125 |
while read bundle; do |
| 126 |
codesign --force --prefix=$MACOSX_BUNDLE_IDENTIFIER. --sign "$MACOSX_CODESIGNING_IDENTITY" "$bundle" || exit 1 |
| 127 |
done |
| 128 |
|
| 129 |
# Quick Look plugins will only load if with sandbox entitlements |
| 130 |
find "$APP_BUNDLE" -name 'QuickLook*.appex' -type d | |
| 131 |
while read appex; do |
| 132 |
codesign --force --timestamp --options=runtime --prefix=$MACOSX_BUNDLE_IDENTIFIER. --sign "$MACOSX_CODESIGNING_IDENTITY" $entitlements_quicklookappex "$appex"|| exit 1 |
| 133 |
done |
| 134 |
|
| 135 |
# Sign executables |
| 136 |
|
| 137 |
find "$APP_BUNDLE/Contents/MacOS" -type f | |
| 138 |
while read file; do |
| 139 |
case "$file" in |
| 140 |
*/soffice) |
| 141 |
;; |
| 142 |
*/unopkg) |
| 143 |
id=`echo ${file#${APP_BUNDLE}/Contents/} | sed -e 's,/,.,g'` |
| 144 |
codesign --force --timestamp --options=runtime --identifier=$MACOSX_BUNDLE_IDENTIFIER.$id --sign "$MACOSX_CODESIGNING_IDENTITY" $entitlements_helper "$file" || exit 1 |
| 145 |
;; |
| 146 |
*/uri-encode) |
| 147 |
id=`echo ${file#${APP_BUNDLE}/Contents/} | sed -e 's,/,.,g'` |
| 148 |
codesign --force --timestamp --options=runtime --identifier=$MACOSX_BUNDLE_IDENTIFIER.$id --sign "$MACOSX_CODESIGNING_IDENTITY" $entitlements_helper "$file" || exit 1 |
| 149 |
;; |
| 150 |
*) |
| 151 |
id=`echo ${file#${APP_BUNDLE}/Contents/} | sed -e 's,/,.,g'` |
| 152 |
codesign --force --timestamp --options=runtime --identifier=$MACOSX_BUNDLE_IDENTIFIER.$id --sign "$MACOSX_CODESIGNING_IDENTITY" $entitlements_helper $launch_constraint "$file" || exit 1 |
| 153 |
;; |
| 154 |
esac |
| 155 |
done |
| 156 |
|
| 157 |
# Sign the app bundle as a whole which means (re-)signing the |
| 158 |
# CFBundleExecutable from Info.plist, i.e. soffice, plus the contents |
| 159 |
# of the Resources tree. |
| 160 |
# |
| 161 |
# See also https://developer.apple.com/library/mac/technotes/tn2206/ |
| 162 |
|
| 163 |
if test -n "$ENABLE_MACOSX_SANDBOX" && test -n "$application_identifier"; then |
| 164 |
# add back the application-identifier to the entitlements |
| 165 |
# testflight/beta-testing won't work if that key is used when signing the other executables |
| 166 |
/usr/libexec/PlistBuddy -c "add com.apple.application-identifier string $application_identifier" $BUILDDIR/lo.xcent |
| 167 |
fi |
| 168 |
codesign --force --timestamp --options=runtime --identifier="${MACOSX_BUNDLE_IDENTIFIER}" --sign "$MACOSX_CODESIGNING_IDENTITY" $entitlements "$APP_BUNDLE" || exit 1 |
| 169 |
|
| 170 |
exit 0 |