Files
Reims-Go/internal/guesttools/drop_agent.sh

272 lines
11 KiB
Bash

drop_helper() {
osascript -l JavaScript "$APP_DIR/drop-resolve.js" "$@"
}
drop_webdav_mounted() {
[ -n "${DROP_MOUNT:-}" ] && [ -n "${REIMS_VM:-}" ] &&
is_reims_dav_mounted "$DROP_MOUNT" "/dav-drop/$(urlencode "$REIMS_VM")/"
}
# Perl cannot prompt for Network Volumes. Use it for heartbeat checks and cached denials.
drop_mount_readable() {
/usr/bin/perl -MErrno=EPERM,EACCES -e '
alarm 2;
sub failed { exit(($! == EPERM || $! == EACCES) ? 2 : 1) }
opendir(my $dir, $ARGV[0]) or failed();
$! = 0;
defined(readdir($dir)) || !$! or failed();
' -- "$DROP_MOUNT" >/dev/null 2>&1
}
probe_drop_mount() (
probe_mode=${1:-probe-mount}
if [ "$#" -gt 0 ]; then shift; fi
marker="$TMP_DIR/drop_mount_denied"
if [ -f "$marker" ]; then
access=0
drop_mount_readable || access=$?
case "$access" in
0) rm -f "$marker" ;;
2) printf '%s\n' denied; return 0 ;;
esac
fi
access=$(drop_helper "$probe_mode" "$DROP_MOUNT" "$@" 2>/dev/null) || access=unhealthy
case "$access" in
ok) rm -f "$marker" ;;
denied) (umask 077; : > "$marker") ;;
*) access=unhealthy ;;
esac
printf '%s\n' "$access"
)
place_drop_unique() (
target_dir=$1 base=$2 part_file=$3
stem=$base ext=
case "$base" in
?*.*) stem=${base%.*}; ext=.${base##*.} ;;
esac
n=0
while [ "$n" -le 999 ]; do
candidate=$base
if [ "$n" -gt 0 ]; then candidate="$stem ($n)$ext"; fi
dest="$target_dir/$candidate"
if [ ! -e "$dest" ] && [ ! -L "$dest" ]; then
if ln "$part_file" "$dest" 2>/dev/null; then
rm -f "$part_file" || return 1
printf '%s\n' "$candidate"
return 0
fi
if [ ! -e "$dest" ] && [ ! -L "$dest" ]; then
mv -n "$part_file" "$dest" 2>/dev/null || :
if [ ! -e "$part_file" ] && [ -f "$dest" ]; then
printf '%s\n' "$candidate"
return 0
fi
[ -e "$dest" ] || [ -L "$dest" ] || return 1
fi
fi
n=$((n + 1))
done
return 1
)
alloc_drop_dir() (
n=0
while [ "$n" -le 999 ]; do
cand=$2
if [ "$n" -gt 0 ]; then cand="$2 ($n)"; fi
if mkdir "$1/$cand" 2>/dev/null; then printf '%s\n' "$cand"; return 0; fi
[ -e "$1/$cand" ] || [ -L "$1/$cand" ] || return 1
n=$((n + 1))
done
return 1
)
copy_drop_tree() (
src_path=$1 target_dir=$2 dst_name=$3
if [ -d "$src_path" ]; then
mapped=$(alloc_drop_dir "$target_dir" "$dst_name") || return 1
cp -R "$src_path/." "$target_dir/$mapped/" || return 1
printf '%s\n' "$mapped"
elif [ -f "$src_path" ]; then
part_file="$target_dir/.reims-drop.$drop_id.$$.part"
(umask 077; set -C; : > "$part_file") || return 1
trap 'rm -f "$part_file"' 0
cp "$src_path" "$part_file" && place_drop_unique "$target_dir" "$dst_name" "$part_file"
else
return 1
fi
)
prune_drop_cache() {
if [ -d "${DROP_CACHE:-}" ]; then
find "$DROP_CACHE" -mindepth 1 -maxdepth 1 -type d -mmin +15 -exec rm -rf {} + 2>/dev/null || true
fi
}
post_drop_ack() {
curl -fsS -m 10 -H "Authorization: Bearer $REIMS_TOKEN" -H 'Content-Type: application/json' \
--data-binary "$1" "$REIMS_HOST_URL/v1/drop-events/ack" >/dev/null
}
process_drop_event() (
umask 077
dirmap="$TMP_DIR/drop_dirmap.$$"
# Regular-file flag checked with -f by clipboard sync; drop_events_loop runs serially.
lock=${DROP_INJECT_LOCK:-$TMP_DIR/drop-injecting}
held=false
trap 'rm -f "$dirmap"; if [ "$held" = true ]; then rm -f "$lock"; fi' 0
trap 'exit 1' HUP INT TERM
touch "$lock" || return 1
held=true
fast=$(REIMS_TOKEN="$REIMS_TOKEN" drop_helper run-drop "$1" "$REIMS_HOST_URL/v1/drop-events/paste") || return 1
rm -f "$lock" || return 1
held=false
exec 3<<EOF
$fast
EOF
tab=$(printf '\t')
IFS= read -r fast_mode <&3 || return 1
case "$fast_mode" in
done"$tab"*)
ack=${fast_mode#done"$tab"}
post_drop_ack "$ack"
return $? ;;
fallback) ;;
*) return 1 ;;
esac
IFS="$tab" read -r drop_id kind webdav_drop display_index norm_x norm_y in_image <&3 || return 1
case "$drop_id" in ''|*[!A-Za-z0-9_-]*) return 1 ;; esac
IFS= read -r target <&3 || return 1
# Split the final field separately so an empty bundle ID survives IFS whitespace folding.
win_num=${target##*"$tab"}
IFS="$tab" read -r t_kind label t_meta <<EOF
${target%"$tab"*}
EOF
set --
error= status=placed
case "$kind/$t_kind" in
files/desktop|files/photos|files/window)
mount_dir=
if [ "$webdav_drop" = true ] && [ -n "${DROP_MOUNT:-}" ]; then
probe_mode=probe-mount
if [ "$t_kind" != desktop ]; then probe_mode=probe-mount-ref; fi
for attempt in first retry; do
if ! drop_webdav_mounted; then
ensure_drop_webdav_mount && drop_webdav_mounted || break
fi
case "$(probe_drop_mount "$probe_mode" "$DROP_MOUNT/$drop_id")" in
ok) mount_dir="$DROP_MOUNT/$drop_id"; break ;;
denied)
ack=$(drop_helper encode-ack "$drop_id" failed "$t_kind" "$label" 'Network Volumes permission denied') || return 1
post_drop_ack "$ack"
return $? ;;
esac
[ "$attempt" = first ] && drop_webdav_mounted &&
unmount_shared_folder "$DROP_MOUNT" >/dev/null 2>&1 || break
done
fi
if [ -z "$mount_dir" ]; then
DROP_CACHE=${DROP_CACHE:-$TMP_DIR/drop-cache}
prune_drop_cache
fi
: > "$dirmap" || return 1
while IFS="$tab" read -r src_item dst_item <&3; do
case "/$src_item/$dst_item/" in *//*|*/./*|*/../*|*'\'*) error='Invalid item name'; break ;; esac
if [ -n "$mount_dir" ]; then
path="$mount_dir/$src_item"
[ -e "$path" ] || { error='Missing drop item'; break; }
else
path="$DROP_CACHE/$drop_id/$dst_item"
mkdir -p "${path%/*}" || { error='Cannot create cache'; break; }
query=$(urlencode "$src_item") || { error='Invalid source'; break; }
url="$REIMS_HOST_URL/v1/file?path=$query"
if [ "$webdav_drop" = true ]; then url="$REIMS_HOST_URL/v1/drop-file?drop_id=$drop_id&path=$query"; fi
if ! curl -fsS --max-time 120 -H "Authorization: Bearer $REIMS_TOKEN" -o "$path" "$url"; then
error='Download failed'; break
fi
fi
if [ "$t_kind" != desktop ]; then set -- "$@" "$path"; continue; fi
target_dir=$t_meta
dest_dir=$target_dir base=$dst_item mapped=
case "$dst_item" in
*/*)
src_top=${src_item%%/*} top=${dst_item%%/*} rest=${dst_item#*/}
while IFS= read -r key && IFS= read -r value; do
if [ "$key" = "$src_top" ]; then mapped=$value; break; fi
done < "$dirmap"
if [ -z "$mapped" ]; then
mapped=$(alloc_drop_dir "$target_dir" "$top") || { error='Cannot create folder'; break; }
printf '%s\n%s\n' "$src_top" "$mapped" >> "$dirmap" || { error='Cannot record folder'; break; }
fi
dest_dir="$target_dir/$mapped"
while :; do
if [ -L "$dest_dir" ]; then error='Invalid folder'; break; fi
case "$rest" in
*/*) dest_dir="$dest_dir/${rest%%/*}"; rest=${rest#*/} ;;
*) break ;;
esac
mkdir "$dest_dir" 2>/dev/null || [ -d "$dest_dir" ] || { error='Cannot create subfolder'; break; }
done
[ -z "$error" ] || break
base=$rest ;;
esac
placed=$(copy_drop_tree "$path" "$dest_dir" "$base") || { error='Placement failed'; break; }
placed=${mapped:-$placed}
seen=false
for existing do [ "$existing" != "$placed" ] || seen=true; done
if [ "$seen" = false ]; then set -- "$@" "$placed"; fi
done
if [ "$#" -eq 0 ] && [ -z "$error" ]; then error='No drop items'; fi ;;
*) status=cancelled ;;
esac
if [ -z "$error" ] && [ "$status" != cancelled ]; then
case "$t_kind" in
photos)
if photos_err=$(drop_helper import-photos "$@" 2>&1 >/dev/null); then :; else
case "$photos_err" in
*1743*|*[Nn]ot\ authorized*|*[Pp]ermission*|*[Pp]rivilege*)
error='Photos import blocked: allow Automation access to Photos in macOS Privacy & Security' ;;
*) error='Photos import failed' ;;
esac
fi
set -- ;;
window)
if touch "$lock"; then
held=true
if res=$(REIMS_TOKEN="$REIMS_TOKEN" drop_helper paste-drop "$display_index" "$norm_x" "$norm_y" \
"$win_num" "$t_meta" "$kind" "$REIMS_HOST_URL/v1/drop-events/paste" "$drop_id" "$@"); then
case "$res" in pasted|cancelled) status=$res ;; *) error='Paste failed' ;; esac
else error='Paste failed'; fi
rm -f "$lock" || return 1
held=false
else error='Cannot lock input'; fi
set -- ;;
esac
fi
if [ -n "$error" ]; then
status=failed
if [ "$t_kind" != desktop ]; then set --; fi
fi
if [ "$status" = cancelled ]; then t_kind=cancelled; label=; fi
ack=$(drop_helper encode-ack "$drop_id" "$status" "$t_kind" "$label" "$error" "$@") || return 1
post_drop_ack "$ack"
)
drop_events_loop() {
ev_file="$TMP_DIR/drop_event"
while :; do
load_conf
if [ -z "$REIMS_HOST_URL" ] || [ -z "$REIMS_TOKEN" ]; then sleep 2; continue; fi
code=$(curl -sS -m 30 -o "$ev_file" -w '%{http_code}' -H "Authorization: Bearer $REIMS_TOKEN" \
"$REIMS_HOST_URL/v1/drop-events?wait=25") || code=000
case "$code" in
200)
process_drop_event "$ev_file" || sleep 1 ;;
204) ;;
*) sleep 1 ;;
esac
done
}