Repository navigation
Expand file tree
/
Copy pathcheckuser
More file actions
executable file
·466 lines (406 loc) · 13.1 KB
/
Copy pathcheckuser
File metadata and controls
executable file
·466 lines (406 loc) · 13.1 KB
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#!/bin/bash
set -o errexit \
-o pipefail \
-o nounset
# Checks the year-based ACLs to see whether a user has reapplied
# for their RC accounts that session.
# use own username if none supplied as argument
username=${1:-$USER}
RESET="$(tput sgr0)"
RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
YELLOW="$(tput setaf 3)"
BLUE="$(tput setaf 4)"
B_RED="$(tput setaf 9)"
B_GREEN="$(tput setaf 10)"
B_YELLOW="$(tput setaf 11)"
B_BLUE="$(tput setaf 12)"
COLOR_ERROR="${UCLRC_COLOR_ERROR:-${RED}}"
COLOR_INFO="${UCLRC_COLOR_INFO:-${BLUE}}"
COLOR_WARN="${UCLRC_COLOR_WARN:-${YELLOW}}"
COLOR_PASS="${UCLRC_COLOR_PASS:-${GREEN}}"
COLOR_FAIL="${UCLRC_COLOR_FAIL:-${RED}}"
function _warn() {
echo "${COLOR_WARN}$*${RESET}"
}
function _error() {
echo "${COLOR_ERROR}$*${RESET}"
}
function _info() {
echo "${COLOR_INFO}$*${RESET}"
}
function _fail() {
echo "${COLOR_FAIL}$*${RESET}"
}
function _palette() {
echo "Palette:"
echo " ${RED}Red${RESET}"
echo " ${GREEN}Green${RESET}"
echo " ${YELLOW}Yellow${RESET}"
echo " ${BLUE}Blue${RESET}"
echo " ${B_RED}Bold Red${RESET}"
echo " ${B_GREEN}Bold Green${RESET}"
echo " ${B_YELLOW}Bold Yellow${RESET}"
echo " ${B_BLUE}Bold Blue${RESET}"
echo "==="
echo " ${COLOR_ERROR}Error${RESET}"
echo " ${COLOR_WARN}Warn${RESET}"
echo " ${COLOR_INFO}Info${RESET}"
echo " ${COLOR_PASS}Pass${RESET}"
echo " ${COLOR_FAIL}Fail${RESET}"
exit
}
# Special case to experiment with colours
if [[ "${1:-}" == "--palette" ]]; then
_palette
fi
### Section: Identity
if [[ "$username" =~ ^[A-Z0-9]+$ ]]; then
_warn "Warning: this username is uppercased. It may be a UPI, not a username."
_warn " It will be converted to lowercase."
_warn " (You may also see this when copy/pasting usernames from MyServices.)"
old_username="$username"
username="${old_username,,}"
echo "Username converted to: ${COLOR_PASS}${username}${RESET}"
fi
if [[ "$username" =~ ^[^[:space:]]+@ucl\.ac\.uk$ ]]; then
_warn "Warning: this username looks like an email address."
_warn " It will be checked and converted."
echo -n "Can id email address: "
id "$username" >/dev/null 2>/dev/null && echo "${COLOR_PASS}yes${RESET}" || echo "${COLOR_WARN}no${RESET}"
old_username="$username"
if username="$(id -un "$username")"; then
echo "Username converted successfully to: ${COLOR_PASS}${username}${RESET}"
else
_warn "Could not convert email address to username, leaving as-is."
username="${old_username}"
fi
fi
echo -n "Can id user: "
id "$username" >/dev/null 2>/dev/null && echo "${COLOR_PASS}yes${RESET}" || echo "${COLOR_FAIL}no${RESET}"
echo -n "User is in groups: "
if user_groups="$(groups "$username" 2>&1)"
then
echo "${COLOR_PASS}${user_groups#*:}${RESET}"
else
echo "${COLOR_FAIL}${user_groups#groups: *:}${RESET}"
fi
echo "" # Blank line for section separation
### Section: Scheduler Registration
function sge_check_acls() {
local access_group
local access_group_label
local -a sge_access_groups sge_access_group_labels
sge_access_groups+=(AY201617)
sge_access_group_labels+=("access group for 2016-2017")
sge_access_groups+=(AY201718)
sge_access_group_labels+=("access group for 2017-2018")
sge_access_groups+=(AY201819)
sge_access_group_labels+=("access group for 2018-2019")
sge_access_groups+=(Open)
sge_access_group_labels+=("Open access group")
for (( access_group_index=0; access_group_index < "${#sge_access_groups[@]}"; access_group_index++ )); do
access_group="${sge_access_groups[$access_group_index]}"
access_group_label="${sge_access_group_labels[$access_group_index]}"
echo -n "Checking whether user is in $access_group_label: "
if qconf -su "$access_group" 2>/dev/null >/dev/null; then
if qconf -su "$access_group" | grep -q "$username"
then
echo "${COLOR_PASS}yes${RESET}"
else
echo "${COLOR_FAIL}no${RESET}"
fi
else
echo "${COLOR_INFO}no such group${RESET}"
fi
done
}
function sge_check_nosub() {
local username="$1"
echo -n "Checking whether user has been blocked from submitting jobs: "
if qconf -su NoSubmission 2>/dev/null >/dev/null
then
# check NoSubmission for blocked users
if qconf -su NoSubmission | grep -q "$username"
then
echo "${COLOR_FAIL}yes${RESET}"
else
echo "${COLOR_PASS}no${RESET}"
fi
else
echo "${COLOR_PASS}no (no blocked ACL here)${RESET}"
fi
}
function slurm_check_user_exists() {
local username="$1"
echo -n "Checking whether user is in Slurm DB: "
command -v jq >/dev/null || echo "${COLOR_FAIL}could not check, jq not found${RESET}"
if sacctmgr --json list user "$username" \
| jq -er '.users[].name' >/dev/null
then
echo "${COLOR_PASS}yes${RESET}"
else
echo "${COLOR_FAIL}no${RESET}"
fi
}
function slurm_check_accounts() {
local username="$1"
local account
local account_desc
local -a check_accounts=(allusers)
local user_accounts
user_accounts="$(sacctmgr show assoc where user="$username" --noheader format='account%-40')"
for account in "${check_accounts[@]}"; do
account_desc="$(sacctmgr show account "$account" --json | jq -r '.accounts[].description')"
echo -n "Checking whether user is in account ${account} (${account_desc}): "
if grep -F -qw "${account}" <<< "${user_accounts}"; then
echo "${COLOR_PASS}yes${RESET}"
else
echo "${COLOR_FAIL}no${RESET}"
fi
done
}
function slurm_check_nosub () {
local username="$1"
local account='allusers'
local maxsubmitjob
echo -n "Checking whether user has been blocked from submitting jobs: "
maxsubmitjob="$(sacctmgr show assoc --noheader --parsable2 \
where account="$account" user="$username" format='maxsubmitjob')"
if [[ "$maxsubmitjob" == '0' ]]; then
echo "${COLOR_FAIL}yes${RESET}"
else
echo "${COLOR_PASS}no${RESET}"
fi
}
function sge_check_access () {
local username="$1"
local pam_listfile="/var/opt/sge/shared/userlist"
echo -n "Checking whether user is in the actual PAM userlist: "
if [[ ! -r "$pam_listfile" ]]; then
echo "${COLOR_FAIL}error${RESET}"
return
fi
if grep "^$username\$" "$pam_listfile" >/dev/null; then
echo "${COLOR_PASS}yes${RESET}"
else
echo "${COLOR_FAIL}no${RESET}"
fi
}
function slurm_check_access () {
local username="$1"
local pag
local user_groups
local clustername
clustername="$(/shared/ucl/sysops/libexec/clustname 2>/dev/null || true)"
echo -n "Checking whether user is in the platform access group: "
if [[ -z "$clustername" ]]; then
echo "${COLOR_FAIL}cannot determine cluster${RESET}"
return
fi
case "$clustername" in
myriad) pag='pag-archpc-myriad' ;;
kathleen) pag='pag-archpc-kathleen' ;;
young) pag='pag-archpc-young' ;;
michael) pag='pag-archpc-michael' ;;
dev-*) pag='pag-archpc-hydra' ;;
*)
echo "${COLOR_FAIL}unknown cluster \"$clustername\"${RESET}"
return
;;
esac
if user_groups="$(groups "$username" 2>&1)"; then
user_groups="${user_groups#*:}"
if grep -F -qw "$pag" <<< "$user_groups"; then
echo "${COLOR_PASS}yes${RESET} ($pag)"
else
echo "${COLOR_FAIL}no${RESET}"
fi
else
echo "${COLOR_FAIL}no${RESET}"
fi
}
function get_user_scratch_dir () {
local username="$1"
local clustername
clustername="$(/shared/ucl/sysops/libexec/clustname 2>/dev/null || true)"
case "$clustername" in
myriad) scratch_dir="/home/${username}/Scratch" ;;
*) scratch_dir="/scratch/scratch/${username}" ;;
esac
echo "${scratch_dir}"
}
have_sge=""
have_slurm=""
if command -v qconf >/dev/null; then
have_sge='yes'
elif command -v sacctmgr >/dev/null; then
have_slurm='yes'
fi
if [[ -n "$have_sge" ]]; then
sge_check_acls "$username"
sge_check_nosub "$username"
sge_check_access "$username"
elif [[ -n "$have_slurm" ]]; then
slurm_check_user_exists "$username"
slurm_check_accounts "$username"
slurm_check_nosub "$username"
slurm_check_access "$username"
fi
echo "" # Blank line for section separation
### Section: Filesystems
# These skip checks that depend on certain dirs existing
flag_has_no_homedir=n
flag_has_no_scratch=n
flag_has_no_acfs=n
user_scratch_dir=$(get_user_scratch_dir "$username")
echo -n "Checking whether user has a home directory: "
if stat --printf='' "/home/${username}" 2>/dev/null
then
echo "${COLOR_PASS}yes${RESET}"
else
echo "${COLOR_FAIL}no${RESET}"
flag_has_no_homedir=y
fi
echo -n "Checking whether user has a scratch directory: "
# On clusters with no discrete scratch FS, ordinary users
# can't stat other users' scratch directories
if [[ -x "$(dirname "$user_scratch_dir")" ]]
then
echo "${COLOR_INFO}inaccessible, skipped${RESET}"
flag_has_no_scratch="y"
elif stat --printf='' "${user_scratch_dir}" 2>/dev/null
then
echo "${COLOR_PASS}yes${RESET}"
else
echo "${COLOR_FAIL}no${RESET}"
flag_has_no_scratch=y
fi
echo -n "Checking whether user has an acfs directory: "
if stat --printf='' "/acfs/users/${username}" 2>/dev/null
then
echo "${COLOR_PASS}yes${RESET}"
else
echo "${COLOR_FAIL}no${RESET}"
flag_has_no_acfs=y
fi
echo -n "Checking whether home directory is *owned* by that user: "
if [[ "$flag_has_no_homedir" == "n" ]]; then
owner="$(stat --printf=%U "/home/${username}" 2>/dev/null)"
if [[ "$owner" == "$username" ]]
then
echo "${COLOR_PASS}yes${RESET}"
else
echo "${COLOR_FAIL}no${RESET}"
fi
else
echo "${COLOR_INFO}skipped${RESET}"
fi
echo -n "Checking whether scratch directory is *owned* by that user: "
if [[ "$flag_has_no_scratch" == "n" ]]
then
owner="$(stat --printf=%U "${user_scratch_dir}" 2>/dev/null)"
if [[ "$owner" == "$username" ]]
then
echo "${COLOR_PASS}yes${RESET}"
else
echo "${COLOR_FAIL}no${RESET}"
fi
else
echo "${COLOR_INFO}skipped${RESET}"
fi
echo -n "Checking whether acfs directory is *owned* by that user: "
if [[ "$flag_has_no_acfs" == "n" ]]; then
owner="$(stat --printf=%U "/acfs/users/${username}" 2>/dev/null)"
if [[ "$owner" == "$username" ]]
then
echo "${COLOR_PASS}yes${RESET}"
else
echo "${COLOR_FAIL}no${RESET}"
fi
else
echo "${COLOR_INFO}skipped${RESET}"
fi
echo -n "Checking whether home directory is usable by owner: "
if [[ "$flag_has_no_homedir" == "n" ]]; then
perms="$(stat --printf=%A "/home/${username}" 2>/dev/null)"
if [[ "${perms:1:3}" =~ rwx ]]
then
echo "${COLOR_PASS}yes${RESET}"
else
echo "${COLOR_FAIL}no: perms are ${perms}${RESET}"
fi
else
echo "${COLOR_INFO}skipped${RESET}"
fi
echo -n "Checking whether scratch directory is usable by owner: "
if [[ "$flag_has_no_scratch" == "n" ]]
then
perms="$(stat --printf=%A "${user_scratch_dir}" 2>/dev/null)"
if [[ "${perms:1:3}" =~ rwx ]];
then
echo "${COLOR_PASS}yes${RESET}"
else
echo "${COLOR_FAIL}no: perms are ${perms}${RESET}"
fi
else
echo "${COLOR_INFO}skipped${RESET}"
fi
echo -n "Checking whether acfs directory is usable by owner: "
if [[ "$flag_has_no_acfs" == "n" ]]; then
perms="$(stat --printf=%A "/acfs/users/${username}" 2>/dev/null)"
if [[ "${perms:1:3}" =~ rwx ]]
then
echo "${COLOR_PASS}yes${RESET}"
else
echo "${COLOR_FAIL}no: perms are ${perms}${RESET}"
fi
else
echo "${COLOR_INFO}skipped${RESET}"
fi
echo -n "Checking whether home directory has standard permissions: "
if [[ "$flag_has_no_homedir" == "n" ]]; then
perms="$(stat --printf=%A "/home/${username}" 2>/dev/null)"
if [[ "${perms}" == "drwx------" ]];
then
echo "${COLOR_PASS}yes${RESET}"
else
echo "${COLOR_FAIL}no: perms are ${perms}${RESET}"
fi
else
echo "${COLOR_INFO}skipped${RESET}"
fi
### Section: User Activity
echo ""
function sge_check_jobs () {
local username="$1"
echo -n "Checking whether user has jobs in the queue: "
num_jobs="$(qstat -u "$username" | wc -l)"
if [[ "$num_jobs" -gt 0 ]]; then
echo "${COLOR_PASS}yes${RESET}"
else
echo "${COLOR_INFO}no${RESET}"
fi
}
function slurm_check_jobs () {
local username="$1"
echo -n "Checking whether user has jobs in the queue: "
num_jobs="$(squeue --noheader --user="$username" --Format='JobID' 2>/dev/null | wc -l)"
if [[ "$num_jobs" -gt 0 ]]; then
echo "${COLOR_PASS}yes${RESET}"
else
echo "${COLOR_INFO}no${RESET}"
fi
}
if [[ -n "$have_sge" ]]; then
sge_check_jobs "$username"
elif [[ -n "$have_slurm" ]]; then
slurm_check_jobs "$username"
fi
echo -n "Checking when user last logged in to this node: "
last_login="$(last -adwn 1 "${username}" | head -n 1)"
if [[ -z "$last_login" ]]; then
echo "${COLOR_FAIL}never${RESET}"
else
echo "${COLOR_INFO}$last_login${RESET}"
fi