|
| 1 | +#!/bin/zsh |
| 2 | + |
| 3 | +FILES_TO_PACKAGE=() |
| 4 | +ROOT_DIRECTORY="" |
| 5 | +PAYLOAD_TARGET="login-once" |
| 6 | +PAYLOAD_IDENTIFIER="" |
| 7 | +OUTPUT_FILE="outset-payload.plist" |
| 8 | +DEBUG=0 |
| 9 | +ENCODE_COUNT=0 |
| 10 | + |
| 11 | +OUTSET_ROOT="/usr/local/outset" |
| 12 | + |
| 13 | +printUsage() { |
| 14 | + echo "OVERVIEW: outset-payload is a utility that package a script for use as an outset payload." |
| 15 | + echo "" |
| 16 | + echo "USAGE: outset-payload --directory <dirname> | --file <filename> [<options>]" |
| 17 | + echo "" |
| 18 | + echo "OPTIONS:" |
| 19 | + echo " -f, --file <filename> Package the selected file" |
| 20 | + echo " additional files can be specified by repeating this option" |
| 21 | + echo " -t, --target <type> Target processing type (default 'login-once')" |
| 22 | + echo " used with --file option to specify the target type" |
| 23 | + echo " -l, --list-targets List all valid target types and exit" |
| 24 | + echo " -d, --directory <dirname> Package all files in the specified directory" |
| 25 | + echo " directory should contain all target types required" |
| 26 | + echo " -c, --create-payload-dirs <dirname> Create the script_payloads directory structure in the" |
| 27 | + echo " specified directory and exit" |
| 28 | + echo " -i, --identifier <name> Unique identifier for the script payload" |
| 29 | + echo " -o, --output <filename> Output filename for the package (default 'outset-payload.plist')" |
| 30 | + echo " -h, --help Print this message" |
| 31 | + echo " --debug Enable debug output" |
| 32 | + echo "" |
| 33 | +} |
| 34 | + |
| 35 | +printValidTargets() { |
| 36 | + targetList=($(ls -d ${OUTSET_ROOT}/login-* ${OUTSET_ROOT}/boot-* ${OUTSET_ROOT}/on-*)) |
| 37 | + echo "Valid targets:" |
| 38 | + for target in ${targetList[@]}; do |
| 39 | + echo " ${target##*/}" |
| 40 | + done |
| 41 | + exit 0 |
| 42 | +} |
| 43 | + |
| 44 | +b64encodefile() { |
| 45 | + local file="$1" |
| 46 | + if [[ -f "$file" ]]; then |
| 47 | + debug "Encoding file: $file" |
| 48 | + /usr/bin/base64 -i "$file" |
| 49 | + fi |
| 50 | +} |
| 51 | + |
| 52 | +createPayloadPlist() { |
| 53 | + local output="$1" |
| 54 | + |
| 55 | + # remove existing plist if it exists |
| 56 | + if [[ -f "$output" ]]; then |
| 57 | + /bin/rm -f "$output" |
| 58 | + fi |
| 59 | + |
| 60 | + # Create an empty plist with top-level dict |
| 61 | + /usr/bin/plutil -create xml1 -- "$output" |
| 62 | + |
| 63 | + if [[ $? -ne 0 ]]; then |
| 64 | + exitWithError "Failed to create plist: $output" |
| 65 | + fi |
| 66 | + |
| 67 | + debug "Created plist: $output" |
| 68 | +} |
| 69 | + |
| 70 | +createPayloadDirs() { |
| 71 | + local dir="$1" |
| 72 | + local targetList=($(ls -d ${OUTSET_ROOT}/login-* ${OUTSET_ROOT}/boot-* ${OUTSET_ROOT}/on-*)) |
| 73 | + |
| 74 | + if [[ -z "$dir" ]]; then |
| 75 | + exitWithError "No directory specified for payload creation.\n\nUsage: --create-payload-dirs <dirname>" |
| 76 | + fi |
| 77 | + |
| 78 | + debug "Creating payload directories in: $dir" |
| 79 | + if [[ ! -d "$dir" ]]; then |
| 80 | + /bin/mkdir -p "$dir" |
| 81 | + if [[ $? -ne 0 ]]; then |
| 82 | + exitWithError "Failed to create directory: $dir" |
| 83 | + fi |
| 84 | + fi |
| 85 | + for target in "${targetList[@]}"; do |
| 86 | + local targetName="${target##*/}" |
| 87 | + local targetDir="$dir/${targetName}" |
| 88 | + debug "Creating directory: $targetDir" |
| 89 | + /bin/mkdir -p "$targetDir" |
| 90 | + if [[ $? -ne 0 ]]; then |
| 91 | + exitWithError "Failed to create directory: $targetDir" |
| 92 | + fi |
| 93 | + done |
| 94 | + exitWithMessage "Payload directories created in: $dir\nPopulate these directories with your scripts and use the --directory option to package them." |
| 95 | +} |
| 96 | + |
| 97 | +addPayload() { |
| 98 | + local output="$1" |
| 99 | + local identifier="$2" |
| 100 | + local target="$3" |
| 101 | + local payloadKey="$4" |
| 102 | + local payload="$5" |
| 103 | + |
| 104 | + debug "Adding payload for identifier: script_payloads${identifier}, target: ${target}, key: ${payloadKey}, output: ${output}" |
| 105 | + |
| 106 | + /usr/libexec/PlistBuddy -c "Add :script_payloads${identifier}:${target}:${payloadKey} string '${payload}'" "$output" |
| 107 | + if [[ $? -ne 0 ]]; then |
| 108 | + exitWithError "Failed to add payload to plist: $output" |
| 109 | + fi |
| 110 | + ENCODE_COUNT=$((ENCODE_COUNT + 1)) |
| 111 | +} |
| 112 | + |
| 113 | +exitWithError() { |
| 114 | + echo "[ERROR] $1\n" |
| 115 | + exit 1 |
| 116 | +} |
| 117 | + |
| 118 | +exitWithMessage() { |
| 119 | + echo "$1" |
| 120 | + exit 0 |
| 121 | +} |
| 122 | + |
| 123 | +debug() { |
| 124 | + # write debug messages to stderr |
| 125 | + if [[ $DEBUG -eq 0 ]]; then |
| 126 | + return |
| 127 | + fi |
| 128 | + datestamp=$(date +"%Y-%m-%d %H:%M:%S") |
| 129 | + echo "DEBUG: $datestamp: $1" >&2 |
| 130 | +} |
| 131 | + |
| 132 | +#printValidTargets |
| 133 | + |
| 134 | +#exit 0 |
| 135 | + |
| 136 | +# if no arguments passed, print help and exit |
| 137 | +if [[ "$#" -eq 0 ]]; then |
| 138 | + printUsage |
| 139 | + exit 0 |
| 140 | +fi |
| 141 | + |
| 142 | +# Loop through named arguments |
| 143 | +while [[ "$#" -gt 0 ]]; do |
| 144 | + case $1 in |
| 145 | + --file|-f) FILES_TO_PACKAGE+=("$2"); shift ;; |
| 146 | + --directory|-d) ROOT_DIRECTORY="$2"; shift ;; |
| 147 | + --target|-t) PAYLOAD_TARGET="$2"; shift ;; |
| 148 | + --list-targets|-l) printValidTargets; exit 0 ;; |
| 149 | + --identifier|-i) PAYLOAD_IDENTIFIER="_${2// /_}"; shift ;; |
| 150 | + --output|-o) OUTPUT_FILE="$2"; shift ;; |
| 151 | + --create-payload-dirs|-c) createPayloadDirs "$2"; exit 0 ;; |
| 152 | + --help|-h|help) printUsage; exit 0 ;; |
| 153 | + --debug) DEBUG=1 ;; |
| 154 | + *) echo "Unknown argument: $1"; printUsage; exit 1 ;; |
| 155 | + esac |
| 156 | + shift |
| 157 | +done |
| 158 | + |
| 159 | +## Start processing: |
| 160 | + |
| 161 | +createPayloadPlist "$OUTPUT_FILE" |
| 162 | + |
| 163 | +# if --files is specified, loop through each, convert to base64 and add to plist |
| 164 | +if [[ ${#FILES_TO_PACKAGE[@]} -gt 0 ]]; then |
| 165 | + debug "Files to package: ${FILES_TO_PACKAGE[@]}" |
| 166 | + for file in "${FILES_TO_PACKAGE[@]}"; do |
| 167 | + if [[ -f "$file" ]]; then |
| 168 | + b64payload=$(b64encodefile "$file") |
| 169 | + if [[ -n "$b64payload" ]]; then |
| 170 | + addPayload "$OUTPUT_FILE" "$PAYLOAD_IDENTIFIER" "$PAYLOAD_TARGET" "$(basename "$file")" "$b64payload" |
| 171 | + else |
| 172 | + exitWithError "Failed to encode file: $file" |
| 173 | + fi |
| 174 | + else |
| 175 | + exitWithError "File not found: $file" |
| 176 | + fi |
| 177 | + done |
| 178 | + exitWithMessage "Plist created at $OUTPUT_FILE with $ENCODE_COUNT files encoded." |
| 179 | +fi |
| 180 | + |
| 181 | +# if --directory is specified, loop through each file in the directory |
| 182 | +# this should contain all target types required |
| 183 | +targetList=($(ls -d ${OUTSET_ROOT}/login-* ${OUTSET_ROOT}/boot-* ${OUTSET_ROOT}/on-*)) |
| 184 | +if [[ -n "$ROOT_DIRECTORY" && -d "$ROOT_DIRECTORY" ]]; then |
| 185 | + for subDir in "${targetList[@]}"; do |
| 186 | + targetName="${subDir##*/}" |
| 187 | + debug "checking "$ROOT_DIRECTORY/$targetName"" |
| 188 | + if [[ -d "$ROOT_DIRECTORY/$targetName" ]]; then |
| 189 | + for file in "$ROOT_DIRECTORY/$targetName"/*; do |
| 190 | + debug "Processing file: $file" |
| 191 | + if [[ -f "$file" ]]; then |
| 192 | + b64payload=$(b64encodefile "$file") |
| 193 | + if [[ -n "$b64payload" ]]; then |
| 194 | + addPayload "$OUTPUT_FILE" "$PAYLOAD_IDENTIFIER" "$targetName" "$(basename "$file")" "$b64payload" |
| 195 | + else |
| 196 | + exitWithError "Failed to encode file: $file" |
| 197 | + fi |
| 198 | + else |
| 199 | + exitWithError "File not found: $file" |
| 200 | + fi |
| 201 | + done |
| 202 | + else |
| 203 | + debug "Source directory not found: $ROOT_DIRECTORY/$targetName" |
| 204 | + fi |
| 205 | + done |
| 206 | + exitWithMessage "Plist created at $OUTPUT_FILE with $ENCODE_COUNT files encoded." |
| 207 | +fi |
0 commit comments