-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathfreeipa-sam.sh
executable file
·152 lines (143 loc) · 4.72 KB
/
freeipa-sam.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
#!/usr/bin/env bash
rcfile="${HOME}/.ipa/freeipa-sam.rc"
if [ -e "$rcfile" ]; then
source "$rcfile"
rcfile_on=true
fi
ssleval=true
prefix=ldaps
passeval() { [ -z $bindpass ] && passeval="UNSET!" || passeval="SET!"; }
ssleval() { [ "$prefix" == "ldaps" ] && ssleval="true" || ssleval="false"; }
actionseval() { [ "$ldapserver" ] && [ "$binduser" ] && [ "$domain" ] && [ "$passeval" == "SET!" ] && actionseval="ready" || actionseval="conditions not yet met" && return 1; }
save() {
test -d "${HOME}/.ipa" || mkdir -p "${HOME}/.ipa"
cat > "${HOME}/.ipa/freeipa-sam.rc" <<EOF
# freeipa-sam rc-file
ldapserver="$ldapserver"
binduser="$binduser"
domain="$domain"
ldapdomain="$ldapdomain"
ssleval=$ssleval
prefix="$prefix"
EOF
}
menu() {
passeval
ssleval
actionseval
clear
echo "\
### FreeIPA - System Account Manager ###
1.) ldapserver=$ldapserver
2.) domain=$domain (ldapdomain=$ldapdomain)
3.) binduser=$binduser
4.) bindpass=$passeval
5.) ssl=$ssleval
Actions ($actionseval):
add | rm | ls | info | passwd | save
--- Results ---
$results
--- End Results ---
"
}
domain2ldapdomain() {
echo "${1}" | awk -F'.' '{for(i=1;i<=NF;i++) printf "dc="$i","; print ""}' | sed 's/,$//'
}
dotask() {
case $1 in
# Setup
1|ldapserver)
read -p "ldapserver=" ldapserver
[ -z $domain ] && domain=${ldapserver#*.} && ldapdomain=$(domain2ldapdomain "$domain")
;;
2|domain)
read -p "domain=" domain
ldapdomain=$(domain2ldapdomain "$domain")
#read -p "ldapdomain=" ldapdomain
;;
3|binduser)
[ -z $domain ] && echo "We need the domain first." && dotask domain
echo "Enter \"mgr\" for Directory Manager. Otherwise enter the username or full binddn (-D option in ldapsearch)"
read -p "binduser=" swap
[ "$swap" == "mgr" ] && binduser='cn=Directory Manager' && return
echo "$swap" | grep '=' -q && binduser="$swap" || binduser="uid=$swap,cn=users,cn=accounts,$ldapdomain"
;;
4|bindpass)
read -sp "Enter password (will not echo): " bindpass
;;
5|ssl)
[ "$prefix" == "ldaps" ] && prefix=ldap || prefix=ldaps
;;
# Actions
# poc)
# results=$(ldapsearch "$prefix""://""$ldapserver" -b "$ldapdomain" -D "$binduser" -w "$bindpass")
# ;;
ls)
results=$(ldapsearch -H "$prefix""://""$ldapserver" -b "cn=sysaccounts,cn=etc,$ldapdomain" -D "$binduser" -w "$bindpass" "(uid=*)" "dn" | grep 'dn: uid')
;;
info)
[ "$2" ] && local uid="$2" || uid="*"
results=$(ldapsearch -H "$prefix""://""$ldapserver" -b "cn=sysaccounts,cn=etc,$ldapdomain" -D "$binduser" -w "$bindpass" "(uid=$uid)" "uid" "memberOf" "passwordExpirationTime")
;;
add)
local uid password
[ "$2" ] && local uid="$2" || read -p "uid of new user=" uid
read -sp "password of new user (blank to generate a password)=" password
[ -z "$password" ] && password=$(randpw) && echo && echo "Generated password: $password"
echo
read -p "password expiration date YYYYMMDD (blank for 20380119)=" expire
[ -z "$expire" ] && expire=20380119
echo -E "\
dn: uid=$uid,cn=sysaccounts,cn=etc,$ldapdomain
changetype: add
objectclass: account
objectclass: simplesecurityobject
uid: $uid
userPassword: $password
passwordExpirationTime: ${expire}031407Z
nsIdleTimeout: 0" | ldapmodify -H "$prefix""://""$ldapserver" -D "$binduser" -w "$bindpass" && results="Submitted." || results="Error."
;;
rm)
local uid
[ "$2" ] && local uid="$2" || read -p "uid of user to remove=" uid
echo -E "\
dn: uid=$uid,cn=sysaccounts,cn=etc,$ldapdomain
changetype: delete" | ldapmodify -H "$prefix""://""$ldapserver" -D "$binduser" -w "$bindpass" && results="Submitted." || results="Error."
;;
passwd)
local uid password
[ "$2" ] && local uid="$2" || read -p "uid of user=" uid
read -sp "new password for user (blank to generate a password)=" password
[ -z "$password" ] && password=$(randpw) && echo && echo "Generated password: $password"
echo
read -p "password expiration date YYYYMMDD (blank for 20380119)=" expire
[ -z "$expire" ] && expire=20380119
echo -E "\
dn: uid=$uid,cn=sysaccounts,cn=etc,$ldapdomain
changetype: modify
replace: userPassword
userPassword: $password
-
replace: passwordExpirationTime
passwordExpirationTime: ${expire}031407Z" | ldapmodify -H "$prefix""://""$ldapserver" -D "$binduser" -w "$bindpass" && results="Submitted." || results="Error."
;;
save)
save
;;
exit)
if [ $rcfile_on == "true" ]; then save; fi
exit
;;
"")
results=""
;;
*)
results="\"$input\" command not found."
esac
}
prompt() { read -p '> ' input; dotask $input; }
randpw() { < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-20};echo;}
while :; do
menu
prompt
done