forked from lzh9102/dokuwiki-authpwauth
/
auth.php
297 lines (261 loc) · 7.56 KB
/
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
if(!defined('DOKU_INC')) die();
/**
* authenticate users using pwauth
*
* @license MIT License (http://opensource.org/licenses/MIT)
* @author Che-Huai Lin <lzh9102@gmail.com>
* @author Jamil Navarro <jamilnavarro@gmail.com>
*/
class auth_plugin_authpwauth extends DokuWiki_Auth_Plugin {
private $pwauth_path;
private $passwd_path;
private $shells_path;
private $email_domain_name;
public function __construct() {
parent::__construct();
// check pwauth executable
$this->pwauth_path = $this->getConf('pwauth_path');
$this->passwd_path = $this->getConf('passwd_path');
$this->shells_path = $this->getConf('shells_path');
$this->email_domain_name = $this->getConf('email_domain_name');
if (is_executable($this->pwauth_path)) {
$this->cando['addUser'] = false;
$this->cando['delUser'] = false;
$this->cando['modLogin'] = false;
$this->cando['modPass'] = false;
$this->cando['modName'] = false;
$this->cando['modMail'] = false;
$this->cando['modGroups'] = false;
if (is_readable($this->passwd_path)) {
$this->cando['getUsers'] = true;
$this->cando['getUserCount'] = true;
} else {
$this->cando['getUsers'] = false;
$this->cando['getUserCount'] = false;
}
$this->cando['getGroups'] = false;
$this->cando['external'] = false;
$this->cando['logout'] = true;
$this->success = true;
} else {
$this->success = false;
echo "pwauth not found!";
}
}
/**
* Check username/password
*
* @return bool
*/
public function checkPass($user, $pass) {
// get user information
$userinfo = posix_getpwnam($user);
if (empty($userinfo)) {
return false;
}
// run pwauth
$handle = popen($this->pwauth_path, "w");
if ($handle === false) {
echo "failed to execute " . $this->pwauth_path . "!";
return false;
}
// write user and password to pwauth
if (fwrite($handle, "$user\n$pass\n") === false) {
echo "failed to write to pwauth!";
return false;
}
// authentication is successful only if the exit status of pwauth is 0
$status = pclose($handle);
if ($status == 0) {
return true;
}
return false;
}
/**
* Return user info
*
* name string full name of the user
* mail string email addres of the user
* grps array list of groups the user is in
*
* @param string $user the user name
* @return array containing user data or false
*/
public function getUserData($user) {
$name = $this->getUserFullName($user);
$mail = $this->getUserMail($user);
$grps = $this->getUserGroups($user);
return array("name" => $name, "mail" => $mail, "grps" => $grps);
}
private function getUserFullName($user) {
$userinfo = posix_getpwnam($user);
if ($userinfo === false) {
return $user; // cannot find user info, use login name as name
}
// gecos is a comma separated list
// the first fields (0) is the user's full name
$gecos = $userinfo["gecos"];
$fields = explode(",", $gecos);
$name = $fields[0];
// if the name is empty, use login name
if ($name == "") {
$name = $user;
}
return $name;
}
public function logOff(){
// do nothing
}
/**
* Return a list of the valid login shells for the system
*
* @author Jamil Navarro <jamilnavarro@gmail.com>
*
* @return array List of valid login shells for the system
*/
private function getSystemShells() {
$out = array();
$handle = fopen($this->shells_path, "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
if (preg_match('/^[^\#]/',$line)) {
$out[] = trim($line);
}
}
}
return $out;
}
/**
* Return a count of the number of user which meet $filter criteria
*
* @author Jamil Navarro <jamilnavarro@gmail.com>
*
* @param array $filter
* @return int
*/
public function getUserCount($filter = array()) {
$handle = fopen($this->passwd_path, "r");
$count = 0;
if ($handle) {
$shells = $this->getSystemShells();
while (($line = fgets($handle)) !== false) {
list($user,$x,$uid,$gid,$GECOS,$home,$shell) = explode(":",trim($line));
// Skip root and service users
if ( ( count($shells) > 0 && !in_array( $shell, $shells)) || $user == "root") {
continue;
}
$info = $this->getUserData($user);
if($this->_applyFilter($user, $info, $filter)) {
$count++;
}
}
}
fclose($handle);
return $count;
}
/*
* Bulk retrieval of user data
*
* @author Jamil Navarro <jamilnavarro@gmail.com>
*
* @param int $start index of first user to be returned
* @param int $limit max number of users to be returned, 0 for unlimited
* @param array $filter array of field/pattern pairs, null for no filter
* @return array list of userinfo (refer getUserData for internal userinfo details)
*/
public function retrieveUsers($start = 0, $limit = 0, $filter = array()) {
$handle = fopen($this->passwd_path, "r");
//return false on bad params
if ( $start < 0 || $limit < 0 ) {
return false;
}
$out = array();
if ($handle) {
$i = 0;
$count = 0;
$shells = $this->getSystemShells();
while (($line = fgets($handle)) !== false) {
list($user,$x,$uid,$gid,$GECOS,$home,$shell) = explode(":",trim($line));
// Skip root and service users
if ( ( count($shells) > 0 && !in_array( $shell, $shells)) || $user == "root") {
continue;
}
$info = $this->getUserData($user);
if($this->_applyFilter($user, $info, $filter)) {
if($i >= $start) {
$out[$user] = $info;
$count++;
if(($limit > 0) && ($count >= $limit)) break;
}
$i++;
}
}
}
fclose($handle);
return $out;
}
/* List all available groups for a user
*
* @param string $user loginname
* @return array|bool false or array with all groups of this user.
*/
private function getUserGroups($user){
// check if the user exists
if (posix_getpwnam($user) === false) {
return false;
}
// use the command "groups <user>" to find the groups
// the format of output is as follows:
// <user> : <group1> <group2> <group3> ...
$output = shell_exec("groups " . escapeshellarg($user));
$output = trim($output); //get rid of newline
// userstring = <user>, groupstring = <group1> <group2> <group3> ...
list($userstring,$groupstring) = explode( ":", $output);
//groups[0] = <group1>, groups[1] = <group2>, groups[2] = <group3>...
$groups = explode(" ",trim($groupstring)); //remove leading space from $groupstring, then split
return $groups;
}
private function getUserMail($user){
// check if the email domain name is set
if ($this->email_domain_name == false) {
return false;
}
// check if the user exists
$userinfo = posix_getpwnam($user);
if ($userinfo === false) {
return false;
}
$username = $userinfo["name"]; // get username from userinfo
$email = $username . "@" . $this->email_domain_name;
return $email;
}
/**
* return true if $user + $info match $filter criteria, false otherwise
*
* @author Jamil Navarro <jamilnavarro@gmail.com>
*
* @param string $user User login
* @param array $info User's userinfo array
* @return bool
*/
function _applyFilter($user, $info, $filter) {
foreach($filter as $key => $pattern) {
//sanitize pattern for use as regex
$pattern = '/'.str_replace('/', '\/', $pattern).'/i';
if($key == 'user') {
if(!preg_match($pattern, $user)) return false;
} else if($key == 'grps') {
if(!count(preg_grep($pattern, $info['grps']))) {
return false;
}
} else {
if(!preg_match($pattern, $info[$key])) {
return false;
}
}
}
return true;
}
}
?>