-
Notifications
You must be signed in to change notification settings - Fork 1
/
radius-user
executable file
·198 lines (152 loc) · 3.6 KB
/
radius-user
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
#! /bin/bash
. /etc/freeradius-bash-admin.conf
_value() {
local DATABASE="$1"
local COLUMN="$2"
_mysql_silent "
SELECT
$COLUMN
FROM $DATABASE
WHERE username = '$USER_NAME';
"
}
_add() {
_prompt USER_NAME "User name"
_prompt FIRST_NAME "First name"
_prompt LAST_NAME "Last name"
_prompt DESCRIPTION "Description"
_prompt PASS_WORD "Password" $(_password_generator)
if [ -z $USER_NAME ] && [ -z $PASS_WORD ]; then
echo "User name and password can not be blank!"
exit 1
fi
_mysql "
INSERT
INTO radcheck (username, attribute, op, value)
VALUES ('$USER_NAME','Cleartext-Password',':=','$PASS_WORD');
INSERT
INTO infouser (username, firstname, lastname, description)
VALUES ('$USER_NAME','$FIRST_NAME','$LAST_NAME','$DESCRIPTION');
"
echo "Those values have been written to the database:"
_show "$USER_NAME"
}
_show() {
local USER_NAME="$1"
if [ -z $USER_NAME ]; then
echo "Usage: radius-user show <username> or <all>"
exit 1
fi
if [ "$USER_NAME" = 'all' ]; then
local AND="AND r.username <> ''"
else
local AND="AND r.username = '$USER_NAME'"
fi
_mysql "
SELECT
r.username AS Username,
r.value AS Password,
u.firstname as Firstname,
u.lastname as Lastname,
u.description as Description
FROM radcheck AS r
JOIN infouser AS u
ON r.username = u.username
WHERE r.attribute = 'Cleartext-Password'
$AND
;
"
}
_update() {
local USER_NAME="$1"
if [ -z "$USER_NAME" ] ; then
echo "Usage: radius-user update <username>"
exit 1
fi
_prompt FIRST_NAME "First name" "$(_value 'infouser' 'firstname')"
_prompt LAST_NAME "Last name" "$(_value 'infouser' 'lastname')"
_prompt DESCRIPTION "Description" "$(_value 'infouser' 'description')"
_prompt PASS_WORD "Password" "$(_value 'radcheck' 'value')"
_mysql "
UPDATE radcheck
SET
value = '$PASS_WORD'
WHERE username = '$USER_NAME'
AND attribute = 'Cleartext-Password';
UPDATE infouser
SET
firstname = '$FIRST_NAME',
lastname = '$LAST_NAME',
description = '$DESCRIPTION'
WHERE username = '$USER_NAME';
"
_show "$USER_NAME"
}
_password() {
local USER_NAME="$1"
if [ -z "$USER_NAME" ] ; then
echo "Usage: radius-user password <username>"
exit 1
fi
_prompt PASS_WORD "Enter password" $(_password_generator)
_mysql "UPDATE radcheck
SET value = '$PASS_WORD'
WHERE username = '$USER_NAME'
AND attribute = 'Cleartext-Password'
"
_show "$USER_NAME"
}
_delete() {
local USER_NAME="$1"
if [ -z "$USER_NAME" ]; then
echo "Usage: radius-user delete <username>"
exit 1
fi
_show "$USER_NAME"
_prompt_yes "Delete user '$USER_NAME'?"
_mysql "
DELETE FROM radcheck WHERE username = '$USER_NAME';
DELETE FROM infouser WHERE username = '$USER_NAME';
"
}
_usage() {
echo "Usage: radius-user <options>
-> Manage the freeradius users.
Options:
add : Add a user.
show <username> or <all> : Show a specific user or all users.
update <username> : Update a user.
password <username> : Update the users' password.
delete <username> : Delete a user.
help : Show this help message.
"
}
case "$1" in
add)
shift 1
_add $@
;;
show)
shift 1
_show $@
;;
update)
shift 1
_update $@
;;
password)
shift 1
_password $@
;;
delete)
shift 1
_delete $@
;;
help)
_usage
;;
*)
_usage
;;
esac
# vim: set ts=2 sw=2 sts=2 et :