-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathejabberd_external_auth.php
More file actions
181 lines (143 loc) · 5.64 KB
/
ejabberd_external_auth.php
File metadata and controls
181 lines (143 loc) · 5.64 KB
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
<?php
/**
* A class extending EjabberdExternalAuth may optionally implement this interface
* to support additional, non-critical methods for adding and removing users.
*/
interface EjabberdExternalAuth_UserManagement {
/**
* Corresponds to `setpass` operation.
*/
public function setPassword($user, $server, $password);
/**
* Corresponds to `tryregister` operation.
*/
public function register($user, $server, $password);
/**
* Corresponds to `removeuser` operation.
*/
public function remove($user, $server);
/**
* Corresponds to `removeuser3` operation.
*/
public function removeSafely($user, $server, $password);
}
abstract class EjabberdExternalAuth {
private $db = null;
private $log = null;
private $stdin = null;
private $stdout = null;
public static $logLevel = array(LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_INFO, LOG_KERN);
/**
* Corresponds to `auth` operation.
*/
abstract protected function authenticate($user, $server, $password);
/**
* Corresponds to `isuser` operation.
*/
abstract protected function exists($user, $server);
final public function __construct(PDO $db = null, $log = null) {
set_error_handler(array($this, 'errorHandler'));
$this->db = $db;
$this->stdin = fopen('php://stdin', 'rb');
$this->stdout = fopen('php://stdout', 'wb');
if ($log) {
$this->log = fopen($log, 'a');
}
$this->log('Starting auth service...', LOG_INFO);
try {
$this->work();
} catch (Exception $e) {
$this->log($e->getMessage());
}
$this->log('Exiting...', LOG_INFO);
}
final protected function db() {
return $this->db;
}
final private function work() {
$this->log('Entering event loop...', LOG_INFO);
while (true) {
try {
$message = $this->read();
$message = $this->parseMessage($message);
} catch (UnexpectedValueException $e) {
$this->log($e->getMessage());
continue;
}
$this->log('Received message: ' . json_encode($message), LOG_DEBUG);
$response = false;
switch ($message['command']) {
case 'auth' :
$response = $this->authenticate($message['user'], $message['server'], $message['password']);
break;
case 'isuser' :
$response = $this->exists($message['user'], $message['server']);
break;
}
if ($this instanceof EjabberdExternalAuth_UserManagement) {
switch ($message['command']) {
case 'setpass' :
$response = $this->setPassword($message['user'], $message['server'], $message['password']);
break;
case 'tryregister' :
$response = $this->register($message['user'], $message['server'], $message['password']);
break;
case 'removeuser' :
$response = $this->remove($message['user'], $message['server']);
break;
case 'removeuser3' :
$response = $this->removeSafely($message['user'], $message['server'], $message['password']);
break;
}
}
$this->respond($response);
}
}
final private function read() {
$length = fgets($this->stdin, 3);
if (feof($this->stdin)) {
throw new RuntimeException('Pipe broken');
}
$length = current(unpack('n', $length));
if (!$length) {
throw new UnexpectedValueException("Invalid length value, won't continue reading");
}
$message = fgets($this->stdin, $length + 1);
return $message;
}
final private function parseMessage($message) {
$message = explode(':', $message, 4);
if (count($message) < 3) {
throw new UnexpectedValueException('Message is too short: ' . join(':', $message));
}
list($command, $user, $server) = $message;
$password = isset($message[3]) ? $message[3] : null;
return compact('command', 'user', 'server', 'password');
}
final private function respond($status) {
$message = pack('nn', 2, (int)$status);
$this->log('Sending response: ' . bin2hex($message), LOG_DEBUG);
fwrite($this->stdout, $message);
}
final protected function log($message, $severity = LOG_ERR) {
if ($this->log && in_array($severity, self::$logLevel, true)) {
static $types = array(
LOG_EMERG => 'EMERGENCY',
LOG_ALERT => 'ALERT',
LOG_CRIT => 'CRITICAL',
LOG_ERR => 'ERROR',
LOG_WARNING => 'WARNING',
LOG_NOTICE => 'NOTICE',
LOG_INFO => 'INFO',
LOG_DEBUG => 'DEBUG',
LOG_KERN => 'KERNEL'
);
$message = sprintf('%s <%s> %9s: %s', date('Y-m-d H:i:s'), getmypid(), isset($types[$severity]) ? $types[$severity] : $types[LOG_ERR], $message);
fwrite($this->log, $message . PHP_EOL);
}
}
final protected function errorHandler($errno, $errstr, $errfile, $errline, array $errcontext) {
$this->log("($errno) $errstr in $errfile on line $errline");
return false;
}
}