-
Notifications
You must be signed in to change notification settings - Fork 2
/
guesspi
executable file
·93 lines (81 loc) · 2.49 KB
/
guesspi
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
#!/bin/bash
# Guess the PI of a user or group
#
# Pseudocode
# ----------
# if argument is user:
# for each allocation:
# print PI # PI is unique per allocation
# else:
# if argument is allocation group:
# print PI # PI is unique
# else if argument is group:
# for each member:
# if ldap uvaPersonIAMAffiliation does not contain "student":
# print # possibly multiple matches in a group
if [ $# -ne 1 ]; then
echo "Usage: `basename $0` user|group"
exit 1
fi
PGPASSWORD=`cat /project/arcs/rc_scripts/PGPASSWORD`
function allocation_pi {
PGPASSWORD=$PGPASSWORD psql -t -h scheduler -U mamreadonly -d mam -c "select ga.g_name,gu.g_name,ga.g_organization,go.g_description,ga.g_description from g_account ga join g_organization go on ga.g_organization=go.g_name join g_account_user gu on ga.g_name=gu.g_account where gu.g_admin='True' and ga.g_active='True' and ga.g_name='$1'" | awk -F'|' '{print $2}'
#RESULT="$(parse_ldap $PI_USERNAME)"
#echo "$RESULT ($1)"
}
function get_field {
echo "$(sed -n "s/^$1: //p" $2)"
}
function join {
local IFS="$1"
shift
printf "%-20s\n" "$*"
}
function parse_ldap {
TMP=$(mktemp)
RESULT=""
ldapsearch -x -LLL -h ldap.virginia.edu -b "o=University of Virginia,c=US" uid=$1 >$TMP
AFFILIATION=( $(get_field uvaPersonIAMAffiliation $TMP) )
if [ "$2" = "group" ]; then
if [[ "$AFFILIATION" =~ "student" ]]; then
rm $TMP
return
fi
fi
DISPLAYNAME=$(get_field displayName $TMP)
printf "%-8s %-35s " "$1" "$DISPLAYNAME"
join , "${AFFILIATION[@]}"
}
#----------------
ARG=$1
ID=$(id $ARG 2>/dev/null)
if [ ! -z "$ID" ]; then
# search user
USERNAME=$ARG
echo "Potential PIs for user $USERNAME:"
ALLOCATIONS=( $(alloc $USERNAME) )
for i in ${ALLOCATIONS[@]}; do
PI_USERNAME=$(allocation_pi $i)
RESULT="$(parse_ldap $PI_USERNAME)"
echo "$RESULT ($1)"
done
else
# test allocation
PI_USERNAME=$(allocation_pi $ARG)
if [ ! -z "$PI_USERNAME" ]; then
echo "PI for allocation $ARG:"
parse_ldap $PI_USERNAME
else
GROUP=$(getent group $ARG)
if [ ! -z "$GROUP" ]; then
# search group
echo "Potential PIs for group $ARG:"
USERS=( $(echo $GROUP | sed 's/^.*://' | tr ',' ' ') )
for USERNAME in ${USERS[@]}; do
parse_ldap $USERNAME group
done
else
echo "Cannot find user or group for $ARG"
fi
fi
fi