-
Notifications
You must be signed in to change notification settings - Fork 7
/
fetch
executable file
·107 lines (79 loc) · 2.86 KB
/
fetch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/sh -e
if [ $# != 2 ]; then
echo "Format: $0 send|recv <msgid>"
exit 1
fi
# Directories
queue=${CABLE_QUEUES}/queue
rqueue=${CABLE_QUEUES}/rqueue
# Parameters
cmd="$1"
msgid="$2"
trap '[ $? = 0 ] || error failed' 0
error() {
logger -t fetch -p mail.err "$@ (${msgid})"
trap - 0
exit 1
}
urlprefix() {
local username=`cat $1/"${msgid}"/username | tr -cd a-z2-7`
local hostname=`cat $1/"${msgid}"/hostname | tr -cd '[:alnum:].-' | tr '[:upper:]' '[:lower:]'`
check_userhost "${username}" "${hostname}"
echo http://"${hostname}"/"${username}"
}
# Sanity checks
[ ${#msgid} = 40 ] || error "bad msgid"
check_userhost() {
[ ${#1} = 32 ] || error "bad username"
[ ${#2} != 0 ] || error "bad hostname"
}
# Retry curl request for 400+ status codes
retrycurl() {
local status= delay=
for delay in 0 5 10 15; do
sleep ${delay}
status=0; curl "$@" || status=$?
if [ ${status} != 22 ]; then
break
fi
done
return ${status}
}
case "${cmd}" in
send)
# <send> [fetch loop]
if [ -e ${queue}/"${msgid}"/${cmd}.req ]; then
prefix=`urlprefix ${queue}`
susername=`cat ${queue}/"${msgid}"/susername | tr -cd '[:alnum:].-' | tr '[:upper:]' '[:lower:]'`
shostname=`cat ${queue}/"${msgid}"/shostname | tr -cd '[:alnum:].-' | tr '[:upper:]' '[:lower:]'`
check_userhost "${susername}" "${shostname}"
curl -sSfg "${prefix}"/request/msg/"${msgid}"/"${shostname}"/"${susername}"
# TODO: with precomputed rpeers, a delay will be much less likely
retrycurl -sSfg -o ${queue}/"${msgid}"/rpeer.sig "${prefix}"/rqueue/"${msgid}".key
# A multi-URI curl command doesn't fail on a bad early fetch
curl -sSfg -o ${queue}/"${msgid}"/ca.pem "${prefix}"/certs/ca.pem
curl -sSfg -o ${queue}/"${msgid}"/verify.pem "${prefix}"/certs/verify.pem
mv ${queue}/"${msgid}"/${cmd}.req ${queue}/"${msgid}"/${cmd}.rdy
else
error "${cmd}.req not found"
fi
;;
recv)
# <recv> [fetch loop]
if [ -e ${rqueue}/"${msgid}"/${cmd}.req -a -e ${rqueue}/"${msgid}"/send.mac -a \
! -e ${rqueue}/"${msgid}"/${cmd}.rdy -a ! -e ${rqueue}/"${msgid}"/${cmd}.ok ]; then
prefix=`urlprefix ${rqueue}`
# A multi-URI curl command deosn't fail on a bad early fetch
curl -sSfg -o ${rqueue}/"${msgid}"/message.enc "${prefix}"/queue/"${msgid}"
curl -sSfg -o ${rqueue}/"${msgid}"/speer.sig "${prefix}"/queue/"${msgid}".key
curl -sSfg -o ${rqueue}/"${msgid}"/ca.pem "${prefix}"/certs/ca.pem
curl -sSfg -o ${rqueue}/"${msgid}"/verify.pem "${prefix}"/certs/verify.pem
mv ${rqueue}/"${msgid}"/${cmd}.req ${rqueue}/"${msgid}"/${cmd}.rdy
else
error "${cmd}.req (without .rdy/.ok) or send.mac not found"
fi
;;
*)
error "unknown command"
;;
esac