-
Notifications
You must be signed in to change notification settings - Fork 0
/
wpa_auth.php
246 lines (206 loc) · 7.36 KB
/
wpa_auth.php
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
#!/usr/bin/env php
<?php
/*
Performs 802.1x authentication on a pfSense system.
*/
require_once('interfaces.inc');
$opts = get_options();
$interface = get_interface_by_name($opts['interface']);
// If a carp interface was provided, this system must be master on that interface.
if (array_key_exists('carp', $opts) && !is_carp_master($opts['carp'])) {
if (!$opts['quiet'])
echo('CARP is running on this system, but interface "' . $opts['carp'] . '" is not a master.' . PHP_EOL);
if (get_supplicant_pid($interface)) {
echo('System is not master, terminating supplicant.' . PHP_EOL);
terminate_supplicant($interface);
}
exit();
}
// Stop here and do nothing if already authenticated, unless --force specified.
if (get_supplicant_status($interface) == 'SUCCESS') {
if (!$opts['force']) {
if (!$opts['quiet'])
echo('Already authenticated. Use --force to restart authentication.' . PHP_EOL);
exit();
} else {
echo('Forcing supplicant termination.' . PHP_EOL);
terminate_supplicant($interface);
}
}
// Ensure only one instance of this script is running from this point forward.
$lock = new Lock();
// Any supplicant running at this point isn't doing anything useful for us.
if (get_supplicant_pid($interface)) {
echo('Supplicant running but not authenticated.' . PHP_EOL . 'Terminating supplicant.' . PHP_EOL);
terminate_supplicant($interface);
}
init_supplicant($interface, $opts['identity'], $opts['password']);
echo('Waiting for authorization.' . PHP_EOL);
while (true) {
switch (get_supplicant_status($interface)) {
case 'SUCCESS':
exit('Authorization completed.' . PHP_EOL);
case 'FAILURE':
terminate_supplicant($interface);
die('Authorization FAILED.');
}
sleep(1);
}
function get_interface_by_name ($interface) {
foreach (get_configured_interface_with_descr() as $name => $desc) {
if (strtolower($interface) == strtolower($desc)) {
$arr = get_interface_info($name);
$arr['name'] = $name;
$arr['desc'] = $interface;
}
}
if (is_null($arr))
die('Interface "' . $interface . '" is not valid.' . PHP_EOL);
elseif ($arr['status'] != 'up')
die('Interface "' . $interface . '" is not up.' . PHP_EOL);
else
return $arr;
}
function is_carp_master ($interface) {
if (!get_carp_status())
die('CARP interface "' . $interface . '" specified, but CARP is not running on this system.' . PHP_EOL);
global $config;
$carp_interface = get_interface_by_name($interface);
foreach ($config['virtualip']['vip'] as $vip) {
if ($carp_interface['name'] == $vip['interface'] && $vip['mode'] == 'carp')
$vip_status = get_carp_interface_status("_vip{$vip['uniqid']}");
}
if (!$vip_status)
die('CARP is not configured for interface "' . $interface . '".' . PHP_EOL);
elseif ($vip_status == 'MASTER')
return true;
}
function get_password_hash ($password) {
if (strlen($password) == 32 && ctype_xdigit($password))
return $password;
else
return hash('md4', mb_convert_encoding($password, 'UTF-16LE'));
}
function get_supplicant_pid($interface) {
return exec('pgrep -f "wpa_supplicant .* ' . $interface['hwif'] . '"');
}
function get_supplicant_status($interface) {
if (get_supplicant_pid($interface))
return exec('wpa_cli status | grep "EAP state" | cut -d= -f2');
}
function terminate_supplicant($interface) {
if ($pid = get_supplicant_pid($interface)) {
exec('wpa_cli -i ' . $interface['hwif'] . ' terminate');
return true;
} else {
return false;
}
}
function init_supplicant($interface, $identity, $password) {
$res = shell_exec('wpa_supplicant -D wired -i ' . $interface['hwif'] . ' -C /var/run/wpa_supplicant -B');
// Start wpa_supplicant
if (strstr($res, 'Successfully initialized wpa_supplicant'))
echo('Started supplicant on interface ' . $interface['hwif'] . ' (' . $interface['desc'] . '/' . $interface['name'] . ').' . PHP_EOL);
else
die('Error initiating the supplicant: ' . PHP_EOL . $res . PHP_EOL);
$auth_params = array(
'ap_scan 0',
'add_network 0',
'set_network 0 key_mgmt IEEE8021X',
'set_network 0 eap PEAP',
'set_network 0 eapol_flags 0',
'set_network 0 phase2 \\"auth=MSCHAPV2\\"',
'set_network 0 identity \\"' . $identity . '\\"',
'set_network 0 password hash:' . $password,
'enable_network 0',
);
echo('Loading parameters to supplicant.' . PHP_EOL);
foreach ($auth_params as $param) {
$res = shell_exec('wpa_cli -i ' . $interface['hwif'] . ' ' . $param);
if (!preg_match('/^(OK|0)$/', $res)) {
echo('Error loading parameter:' . PHP_EOL . $res . PHP_EOL);
terminate_supplicant($interface);
die('Supplicant terminated.' . PHP_EOL);
}
}
echo('Finished loading parameters.' . PHP_EOL);
return true;
}
function get_options () {
$help = (PHP_EOL .
'Usage: ' . basename(__FILE__) . ' [-i interface] [-u identity] [-p password]' . PHP_EOL
. PHP_EOL
. 'Performs 802.1x authentication on a pfSense system.' . PHP_EOL
. 'Usage of a hashed password is highly recommended, but not mandatory.' . PHP_EOL
. PHP_EOL
. ' -i, --interface string Interface to authenticate' . PHP_EOL
. ' -c, --carp string Run only if this system is a CARP master for this interface' . PHP_EOL
. ' -u, --identity string Identity (i.e. username)' . PHP_EOL
. ' -p, --password string Password' . PHP_EOL
. ' --hash string Converts plaintext password to a hash' . PHP_EOL
. ' --force Force run, even if already successfully authenticated' . PHP_EOL
. ' -q, --quiet Suppress output' . PHP_EOL
. ' -h, --help Print usage' . PHP_EOL
. PHP_EOL
);
$opts = getopt(
'i:c:u:p:qh',
array(
'interface:',
'carp:',
'identity:',
'password:',
'hash:',
'force',
'quiet',
'help',
)
);
if (array_key_exists('h', $opts) || array_key_exists('help', $opts))
exit($help);
if (array_key_exists('hash', $opts))
exit(get_password_hash($opts['hash']) . PHP_EOL);
if (array_key_exists('i', $opts))
$opts['interface'] = $opts['i'];
if (array_key_exists('c', $opts))
$opts['carp'] = $opts['c'];
if (array_key_exists('u', $opts))
$opts['identity'] = $opts['u'];
if (array_key_exists('p', $opts))
$opts['password'] = $opts['p'];
if (array_key_exists('q', $opts) || array_key_exists('quiet', $opts))
$opts['quiet'] = true;
if (array_key_exists('force', $opts))
$opts['force'] = true;
if (!array_key_exists('interface', $opts))
die('ERROR: interface is a required parameter.' . PHP_EOL . $help);
if (!array_key_exists('identity', $opts))
die('ERROR: identity is a required parameter.' . PHP_EOL . $help);
if (!array_key_exists('password', $opts))
die('ERROR: password is a required parameter.' . PHP_EOL . $help);
else {
$opts['password'] = get_password_hash($opts['password']);
}
// We don't need these anymore.
unset (
$opts['i'],
$opts['c'],
$opts['u'],
$opts['p'],
$opts['q']
);
return $opts;
}
class Lock {
private $fp;
function __construct() {
$this->fp = fopen(__FILE__, 'r');
if (!flock($this->fp, LOCK_EX|LOCK_NB))
die('Another instance of ' . basename(__FILE__) . ' is already running.' . PHP_EOL);
}
function __destruct() {
flock($this->fp, LOCK_UN);
fclose($this->fp);
}
}
?>