-
Notifications
You must be signed in to change notification settings - Fork 7
/
comm
executable file
·111 lines (87 loc) · 2.59 KB
/
comm
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
108
109
110
111
#!/bin/sh -e
if [ $# != 2 ]; then
echo "Format: $0 send|recv|ack|fin <msgid>"
exit 1
fi
# Directories and files
username=`cat ${CABLE_CERTS}/certs/username | tr -cd a-z2-7`
queue=${CABLE_QUEUES}/queue
rqueue=${CABLE_QUEUES}/rqueue
# Parameters
cmd="$1"
msgid="$2"
trap '[ $? = 0 ] || error failed' 0
error() {
logger -t comm -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}"/request
}
# MAC key extractor
getmac() {
local src="$1" mac=
mac=`cat "${src}" | tr -cd '[:xdigit:]' | tr A-F a-f`
[ ${#mac} = 128 ] || error "malformed or non-existing MAC in `basename "${src}"`"
echo "${mac}"
}
# Sanity checks
[ ${#msgid} = 40 ] || error "bad msgid"
[ ${#username} = 32 ] || error "bad own username"
check_userhost() {
[ ${#1} = 32 ] || error "bad username"
[ ${#2} != 0 ] || error "bad hostname"
}
case "${cmd}" in
send)
# <send> [comm loop]
if [ -e ${queue}/"${msgid}"/${cmd}.ok -a ! -e ${queue}/"${msgid}"/ack.ok ]; then
prefix=`urlprefix ${queue}`
sendmac=`getmac ${queue}/"${msgid}"/send.mac`
curl -sSfg "${prefix}"/snd/"${msgid}"/"${sendmac}"
else
error "${cmd}.ok (without ack.ok) not found"
fi
;;
recv)
# NOTE: dir can be renamed at any moment by <fin>[service]
# <recv> [comm loop]
if [ -e ${rqueue}/"${msgid}"/${cmd}.ok ]; then
prefix=`urlprefix ${rqueue}`
recvmac=`getmac ${rqueue}/"${msgid}"/recv.mac`
curl -sSfg "${prefix}"/rcp/"${msgid}"/"${recvmac}"
else
error "${cmd}.ok not found"
fi
;;
ack)
# <ack> [comm loop]
if [ -e ${queue}/"${msgid}"/${cmd}.ok ]; then
prefix=`urlprefix ${queue}`
ackmac=`getmac ${queue}/"${msgid}"/ack.mac`
curl -sSfg "${prefix}"/ack/"${msgid}"/"${ackmac}"
mv -T ${queue}/"${msgid}" ${queue}/"${msgid}".del
# try to run 2nd ack variant immediately
# rm -r --one-file-system ${queue}/"${msgid}".del
elif [ -e ${queue}/"${msgid}".del ]; then
rm -r --one-file-system ${queue}/"${msgid}".del
else
error "${cmd}.ok or .del directory not found"
fi
;;
fin)
# <fin> [comm loop]
if [ -e ${rqueue}/"${msgid}".del ]; then
rm -r --one-file-system ${rqueue}/"${msgid}".del
else
error ".del directory not found"
fi
;;
*)
error "unknown command"
;;
esac