forked from svaithee12/freeipa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinactive-users.sh
49 lines (43 loc) · 2.19 KB
/
inactive-users.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
#!/bin/bash
####################################################################
# Created by Martin Tyrefors Branden ([email protected]) #
####################################################################
# Suggestion for crontab:
# 00 00 * * * /bin/bash path/to/your/script.sh
# Set a user that is allowed to do LDAP searches.
LDAP_USER=""
LDAP_PASSWORD=""
LDAP_DOMAIN="dc=example,dc=com"
LDAP_SERVER="ipa.example.com"
USERS=`ldapsearch -D "uid=$LDAP_USER,cn=users,cn=accounts,$LDAP_DOMAIN" -w '$LDAP_PASSWORD' -b "cn=accounts,$LDAP_DOMAIN" -h $LDAP_SERVER | grep "User private group for" | awk '{print $6}'`
for USER in $USERS; do
LAST_SUCCESSFUL_AUTH=`ldapsearch -D "uid=$LDAP_USER,cn=users,cn=accounts,$LDAP_DOMAIN" -w '$LDAP_PASSWORD' -b "uid=$USER,cn=users,cn=accounts,$LDAP_DOMAIN" -h $LDAP_SERVER | grep krbLastSuccessfulAuth | awk '{print $2}' | cut -c1-8`
INACTIVE_LIMIT="30"
LOCKOUT_LIMIT="90"
LIMIT_DATE="$(date "+%Y%m%d" -d "$INACTIVE_LIMIT days ago")"
LOCKOUT_DATE="$(date "+%Y%m%d" -d "$LOCKOUT_LIMIT days ago")"
if [ "$LAST_SUCCESSFUL_AUTH" != "" ]; then
if [ "$LOCKOUT_DATE" -gt "$LAST_SUCCESSFUL_AUTH" ]; then
echo "$USER has been inactive for at least $LOCKOUT_LIMIT days."
LOCKOUT_USER="$LOCKOUT_USER $USER"
elif [ "$LIMIT_DATE" -gt "$LAST_SUCCESSFUL_AUTH" ]; then
echo "User has been inactive for more than 30 days."
echo "Disabling user..."
DISABLE_EXEC=$(ipa user-disable $USER)
else
echo "$USER is active, moving on..."
fi
else
echo $USER "has never logged on"
fi
done
if [ "$LOCKOUT_USER" != "" ]; then
SUBJECT="Users inactive for at least $LOCKOUT_LIMIT in FreeIPA"
EMAIL_ADDRESS="[email protected]"
for LUSER in $LOCKOUT_USER; do
FULLNAME=`getent passwd $LUSER | cut -d ':' -f 5`
echo "$LUSER - $FULLNAME" >> /tmp/ipamail.txt
done
send_email=`/bin/mail -s "$SUBJECT" "$EMAIL_ADDRESS" < /tmp/ipamail.txt`
rm -rf /tmp/ipamail.txt
fi