-
Notifications
You must be signed in to change notification settings - Fork 0
/
watchgate.sh
304 lines (302 loc) · 9.11 KB
/
watchgate.sh
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
watchgate.substitute()
{
local reslist devlist libdir includedir bindir cmd i signal \
systemddir mandir seedprefix configdir queryscript cronscript prefix \
cmdlist='sed basename cat id cut bash man mktemp egrep
date env mv chpasswd pwgen sudo cp chmod ln chown rm sha1sum
sha512sum gpg shred mkdir systemctl tty stat head cut tr groups
gpasswd'
declare -A Devlist=(
)
cmdlist="${Devlist[@]} $cmdlist"
for cmd in $cmdlist;do
i=($(\builtin type -afp $cmd))
[[ -z $i ]] && {
[[ -z ${Devlist[$cmd]} ]] && reslist+=" $cmd" || devlist+=" $cmd"
}
\builtin eval "local ${cmd//-/_}=${i:-:}"
done
[[ -n $reslist ]] && {
\builtin printf "%s\n" "$FUNCNAME Require: $reslist"
return
}
[[ -n $devlist ]] && \builtin printf "%s\n" "$FUNCNAME Optional: $devlist"
libdir=/usr/local/lib
includedir=/usr/local/include/
bindir=/usr/local/bin/
prefix=/usr/local/bin/
cronscript=watchgate.cron
queryscript=watchgate
configdir=/etc/watchgate/
seedprefix=watchgate_${HOSTNAME}
mandir=/usr/local/man/man1/
systemddir=/usr/lib/systemd/system/
signal='HUP TERM INT RETURN'
builtin source <($cat<<-SUB
watchgate.server()
{
local seed="${configdir}${seedprefix}"
local user=\${1:?[user]}
[[ \$($id -u) != 0 ]] && return
$egrep -q "^\${user}:" /etc/passwd || return
if [[ -a \$seed ]];then
$pwgen --capitalize --numerals --num-passwords=1 \
--secure --sha1=\$seed#"\$user\$($date -u +"%Y%m%d%H%M")" 8
return
fi
builtin printf "Seed missing!\n"
$pwgen --capitalize --numerals --num-passwords=1 \
--secure --sha1=/dev/null#"\$user\$($date -u +"%Y%m%d%H%M")" 8
}
watchgate()
{
local help="\${FUNCNAME}:[user][optional: stored seed asc file]"
local user=\${1:?"\${help}"}
local seed=\${2:-"${configdir}${seedprefix}"}
seed=\${seed%.asc}
$egrep -q "^\${user}:" /etc/passwd
if [[ \$? != 0 ]];then
\builtin printf \${help}
return
fi
if [[ -r \${2} || -a \$seed && -r \$seed.asc ]];then
# \builtin set -x
\builtin declare -x GPG_TTY="\$($tty)"
local owner=\$($stat -c %U \$GPG_TTY)
if [[ "\${owner}" != "\${USER}" ]];then
\builtin printf "User: \"\${USER}\" isn't the owner of \${GPG_TTY}.\n"
return
fi
\builtin \shopt -s extdebug
\builtin trap "watchgate.delocate" $signal
watchgate.delocate()
{
[[ -r \${tmpfile} ]] && $shred -fu \$tmpfile
\builtin trap - $signal
\builtin shopt -u extdebug
\builtin unset -f watchgate.delocate
\builtin set +x
}
local tmpfile=\$($mktemp)
$gpg --homedir \$HOME/.gnupg --no-tty \
--decrypt --no-verbose --quiet \$seed.asc >\$tmpfile
[[ \$? != 0 ]] && return
$pwgen --capitalize --numerals --num-passwords=1 \
--secure --sha1=\$tmpfile#"\$user\$($date -u +"%Y%m%d%H%M")" 8
return
fi
builtin printf "Seed missing!\n"
$pwgen --capitalize --numerals --num-passwords=1 \
--secure --sha1=/dev/null#"\$user\$($date -u +"%Y%m%d%H%M")" 8
}
watchgate.cron()
{
#set -x
[[ \$($id -u) != 0 ]] && return
local seed="${configdir}${seedprefix}"
local loginuser=\${1:?[login user]}
local i user word timestamp
local tmpfile=/tmp/\${RANDOM}
if [[ ! -r \$seed || ! -r \$seed.asc ]];then
seed=/dev/null
builtin printf "Seed missing!\n"
fi
\builtin trap "[[ -r \$tmpfile ]] && $shred -fu \$tmpfile" SIGHUP SIGTERM SIGINT
declare -a Users=(\$($egrep -v "\${loginuser}|nologin\$|false\$" /etc/passwd |\
$cut -d':' -f1))
timestamp=\$($date -u +"%Y%m%d%H%M")
for user in \${Users[@]};do
\builtin printf "\$user:" >>\$tmpfile
$pwgen --capitalize --numerals \
--num-passwords=1 --secure --sha1=\$seed#\$user\$timestamp 8 >>\$tmpfile
done
$chpasswd <\$tmpfile
$shred -fu \$tmpfile
#set +x
}
watchgate.cron.install()
{
local loginuser=\${1:?[login user]}
local fun='watchgate.cron'
local script="$prefix/\${fun}"
\builtin type -t \${fun} || return
$rm -f \${script}
$cat <<-WATCHGATECRONINSTALL > \${script}
#!$env $bash
\$(\builtin declare -f \${fun})
\${fun} "\${loginuser}"
WATCHGATECRONINSTALL
$sudo $chmod u=rx,go= \${script}
$sudo $chown root:users \${script}
\builtin unset -f \${fun}
}
watchgate.query()
{
local fun='watchgate'
local script="$prefix/\${fun}"
\builtin type -t \${fun} || return
$rm -f \${script}
$cat <<-WATCHGATEQUERY > \${script}
#!$env $bash
\$(\builtin declare -f \${fun})
\${fun} "\\\$@"
WATCHGATEQUERY
$chmod gu=rx,o= \${script}
$chown $USER:users \${script}
\builtin unset -f \${fun}
}
watchgate.install()
{
local prefix
[[ \$($basename \${PWD}) == watchgate ]] && prefix='src/'
$egrep -q "^users:.*\${USER}" /etc/group
if [[ \$? -ne 0 ]];then
$sudo $gpasswd -a ${USER} users
\builtin printf "%s\n" "please logout,
login and run watchgate.install again."
return
fi
watchgate.uninstall
watchgate.cron.install \${1:?[login user]}
watchgate.query
$sudo $mkdir -p $mandir
$sudo $chmod 0755 $mandir
$sudo $cp \${prefix}watchgate.1 \
$mandir/watchgate.1
$sudo $chmod 0644 $mandir/watchgate.1
$sudo $chown $USER:users \
$mandir/watchgate.1
$sudo $cp \${prefix}watchgate.service \
$systemddir/watchgate.service
$sudo $chmod 0644 \
$systemddir/watchgate.service
$sudo $cp \${prefix}watchgate.timer \
$systemddir/watchgate.timer
$sudo $chmod 0644 \
$systemddir/watchgate.timer
$sudo $ln -s $systemddir/watchgate.timer \
$systemddir/timers.target.wants/watchgate.timer
}
watchgate.uninstall()
{
$sudo $rm -f $systemddir/watchgate.service
$sudo $rm -f $systemddir/watchgate.timer
$sudo $rm -f \
$systemddir/timers.target.wants/watchgate.timer
$sudo $rm -f \
/var/lib/systemd/timers/stamp-watchgate.timer
$sudo $rm -f \
$prefix$queryscript
$sudo $rm -f \
$prefix$cronscript
$sudo $rm -f $mandir/watchgate.1
watchgate.seed.uninstall
}
watchgate.seed()
{
local destdir=\${1:?[seed dest dir]}
[[ -d \$destdir ]] || return
local seed="${seedprefix}_\$($date -u +"%Y%m%d%H%M%S")"
local tmpfile=\$($mktemp)
\builtin trap "$shred -fu \$tmpfile;$sudo $chown \${owner}: \$GPG_TTY" \
SIGHUP SIGTERM SIGINT
$sha512sum <<<"\$RANDOM\$RANDOM\$RANDOM\$RANDOM" | $cut -d' ' -f1 >\$tmpfile
\builtin declare -x GPG_TTY="\$($tty)"
local owner=\$($stat -c %U \$GPG_TTY)
$sudo $chown \$USER: \$GPG_TTY
$gpg --symmetric --no-verbose --quiet \
--output \$destdir/\$seed.asc --armor \$tmpfile
$shred -fu \$tmpfile
$chmod 0400 \$destdir/\$seed.asc
$sudo $chown \${owner}: \$GPG_TTY
}
watchgate.seed.install()
{
local seedasc=\${1:?[watchgate_\$hostname_\$date.asc file]}
local seed=\$($basename \${seedasc%.asc})
local destseed=$configdir/\$seed
local owner=\$($stat -c %U \$GPG_TTY)
# \builtin set -x
\builtin \shopt -s extdebug
\builtin declare -x GPG_TTY="\$($tty)"
\builtin trap "watchgate.delocate" $signal
watchgate.delocate()
{
[[ -r \${tmpfile} ]] && $shred -fu \$tmpfile
[[ "\${owner}" == "\${USER}" ]] || $sudo $chown \${owner}: \$GPG_TTY
\builtin trap - signal
\builtin shopt -u extdebug
\builtin unset -f watchgate.delocate
\builtin set +x
}
local tmpfile=\$($mktemp)
$sudo $chown \$USER: \$GPG_TTY
$gpg --no-tty --decrypt --no-verbose --quiet \$seedasc >\$tmpfile
[[ \$? != 0 ]] && return
$sudo $mkdir -p $configdir
$sudo $chmod ug=rx,o= $configdir
$sudo $chown root:users $configdir
[[ -a \$destseed ]] && $sudo $shred -fu \$destseed
[[ -a \$destseed.asc ]] && $sudo $shred -fu \$destseed.asc
$sudo $cp -f \$seedasc \$destseed.asc
$sudo $mv -f \$tmpfile \$destseed
$sudo $chmod 0440 \$destseed.asc
$sudo $chown root:users \$destseed.asc
$sudo $chmod 0400 \$destseed
$sudo $chown root:root \$destseed
$sudo $ln -fs \$destseed ${configdir}${seedprefix}
$sudo $ln -fs \$destseed.asc $configdir$seedprefix.asc
}
watchgate.seed.uninstall()
{
$sudo $rm -f $configdir$seedprefix.asc
$sudo $rm -f $configdir$seedprefix
$sudo $rm -f $configdir$seedprefix_*.asc
$sudo $rm -f $configdir$seedprefix_*
}
watchgate.enable()
{
$sudo $systemctl enable watchgate.timer
}
watchgate.start()
{
$sudo $systemctl start watchgate.timer
watchgate.timer
}
watchgate.stop()
{
$sudo $systemctl stop watchgate.timer
watchgate.timer
}
watchgate.disable()
{
$sudo $systemctl disable watchgate.timer
watchgate.timer
}
watchgate.mask()
{
$sudo $systemctl mask watchgate.timer
watchgate.timer
}
watchgate.unmask()
{
$sudo $systemctl unmask watchgate.timer
watchgate.timer
}
watchgate.reload()
{
$sudo $systemctl daemon-reload
}
watchgate.units()
{
$sudo $systemctl list-units
}
watchgate.timer()
{
$sudo $systemctl list-timers --all
}
SUB
)
}
watchgate.substitute
builtin unset -f watchgate.substitute