| 1 |
lars |
1 |
<?php
|
|
|
2 |
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
|
|
3 |
|
|
|
4 |
/**
|
|
|
5 |
* File::Passwd::Cvs
|
|
|
6 |
*
|
|
|
7 |
* PHP versions 4 and 5
|
|
|
8 |
*
|
|
|
9 |
* LICENSE: This source file is subject to version 3.0 of the PHP license
|
|
|
10 |
* that is available through the world-wide-web at the following URI:
|
|
|
11 |
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
|
|
|
12 |
* the PHP License and are unable to obtain it through the web, please
|
|
|
13 |
* send a note to license@php.net so we can mail you a copy immediately.
|
|
|
14 |
*
|
|
|
15 |
* @category FileFormats
|
|
|
16 |
* @package File_Passwd
|
|
|
17 |
* @author Michael Wallner <mike@php.net>
|
|
|
18 |
* @copyright 2003-2005 Michael Wallner
|
|
|
19 |
* @license http://www.php.net/license/3_0.txt PHP License 3.0
|
|
|
20 |
* @version CVS: $Id: Cvs.php,v 1.14 2005/03/30 18:33:33 mike Exp $
|
|
|
21 |
* @link http://pear.php.net/package/File_Passwd
|
|
|
22 |
*/
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* Requires File::Passwd::Common
|
|
|
26 |
*/
|
|
|
27 |
require_once 'File/Passwd/Common.php';
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Manipulate CVS pserver passwd files.
|
|
|
31 |
*
|
|
|
32 |
* <kbd><u>
|
|
|
33 |
* A line of a CVS pserver passwd file consists of 2 to 3 colums:
|
|
|
34 |
* </u></kbd>
|
|
|
35 |
* <pre>
|
|
|
36 |
* user1:1HCoDDWxK9tbM:sys_user1
|
|
|
37 |
* user2:0O0DYYdzjCVxs
|
|
|
38 |
* user3:MIW9UUoifhqRo:sys_user2
|
|
|
39 |
* </pre>
|
|
|
40 |
*
|
|
|
41 |
* If the third column is specified, the CVS user named in the first column is
|
|
|
42 |
* mapped to the corresponding system user named in the third column.
|
|
|
43 |
* That doesn't really affect us - just for your interest :)
|
|
|
44 |
*
|
|
|
45 |
* <kbd><u>Output of listUser()</u></kbd>
|
|
|
46 |
* <pre>
|
|
|
47 |
* array
|
|
|
48 |
* + user => array
|
|
|
49 |
* + passwd => crypted_passwd
|
|
|
50 |
* + system => system_user
|
|
|
51 |
* + user => array
|
|
|
52 |
* + passwd => crypted_passwd
|
|
|
53 |
* + system => system_user
|
|
|
54 |
* </pre>
|
|
|
55 |
*
|
|
|
56 |
* @author Michael Wallner <mike@php.net>
|
|
|
57 |
* @package File_Passwd
|
|
|
58 |
* @version $Revision: 1.14 $
|
|
|
59 |
* @access public
|
|
|
60 |
*/
|
|
|
61 |
class File_Passwd_Cvs extends File_Passwd_Common
|
|
|
62 |
{
|
|
|
63 |
/**
|
|
|
64 |
* Constructor
|
|
|
65 |
*
|
|
|
66 |
* @access public
|
|
|
67 |
*/
|
|
|
68 |
function File_Passwd_Cvs($file = 'passwd')
|
|
|
69 |
{
|
|
|
70 |
parent::__construct($file);
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* Fast authentication of a certain user
|
|
|
75 |
*
|
|
|
76 |
* Returns a PEAR_Error if:
|
|
|
77 |
* o file doesn't exist
|
|
|
78 |
* o file couldn't be opened in read mode
|
|
|
79 |
* o file couldn't be locked exclusively
|
|
|
80 |
* o file couldn't be unlocked (only if auth fails)
|
|
|
81 |
* o file couldn't be closed (only if auth fails)
|
|
|
82 |
*
|
|
|
83 |
* @static call this method statically for a reasonable fast authentication
|
|
|
84 |
* @access public
|
|
|
85 |
* @return mixed true if authenticated, false if not or PEAR_Error
|
|
|
86 |
* @param string $file path to passwd file
|
|
|
87 |
* @param string $user user to authenticate
|
|
|
88 |
* @param string $pass plaintext password
|
|
|
89 |
*/
|
|
|
90 |
function staticAuth($file, $user, $pass)
|
|
|
91 |
{
|
|
|
92 |
$line = File_Passwd_Common::_auth($file, $user);
|
|
|
93 |
if (!$line || PEAR::isError($line)) {
|
|
|
94 |
return $line;
|
|
|
95 |
}
|
|
|
96 |
@list(,$real) = explode(':', $line);
|
|
|
97 |
return (File_Passwd_Cvs::generatePassword($pass, $real) === $real);
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* Apply changes and rewrite CVS passwd file
|
|
|
102 |
*
|
|
|
103 |
* Returns a PEAR_Error if:
|
|
|
104 |
* o directory in which the file should reside couldn't be created
|
|
|
105 |
* o file couldn't be opened in write mode
|
|
|
106 |
* o file couldn't be locked exclusively
|
|
|
107 |
* o file couldn't be unlocked
|
|
|
108 |
* o file couldn't be closed
|
|
|
109 |
*
|
|
|
110 |
* @throws PEAR_Error
|
|
|
111 |
* @access public
|
|
|
112 |
* @return mixed true on success or PEAR_Error
|
|
|
113 |
*/
|
|
|
114 |
function save()
|
|
|
115 |
{
|
|
|
116 |
$content = '';
|
|
|
117 |
foreach ($this->_users as $user => $v){
|
|
|
118 |
$content .= $user . ':' . $v['passwd'];
|
|
|
119 |
if (isset($v['system']) && !empty($v['system'])) {
|
|
|
120 |
$content .= ':' . $v['system'];
|
|
|
121 |
}
|
|
|
122 |
$content .= "\n";
|
|
|
123 |
}
|
|
|
124 |
return $this->_save($content);
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* Parse the CVS passwd file
|
|
|
129 |
*
|
|
|
130 |
* Returns a PEAR_Error if passwd file has invalid format.
|
|
|
131 |
*
|
|
|
132 |
* @throws PEAR_Error
|
|
|
133 |
* @access public
|
|
|
134 |
* @return mixed true on success or PEAR_Error
|
|
|
135 |
*/
|
|
|
136 |
function parse()
|
|
|
137 |
{
|
|
|
138 |
$this->_users = array();
|
|
|
139 |
foreach ($this->_contents as $line) {
|
|
|
140 |
$user = explode(':', $line);
|
|
|
141 |
if (count($user) < 2) {
|
|
|
142 |
return PEAR::raiseError(
|
|
|
143 |
FILE_PASSWD_E_INVALID_FORMAT_STR,
|
|
|
144 |
FILE_PASSWD_E_INVALID_FORMAT
|
|
|
145 |
);
|
|
|
146 |
}
|
|
|
147 |
@list($user, $pass, $system) = $user;
|
|
|
148 |
$this->_users[$user]['passwd'] = $pass;
|
|
|
149 |
if (!empty($system)) {
|
|
|
150 |
$this->_users[$user]['system'] = $system;
|
|
|
151 |
}
|
|
|
152 |
}
|
|
|
153 |
$this->_contents = array();
|
|
|
154 |
return true;
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
/**
|
|
|
158 |
* Add an user
|
|
|
159 |
*
|
|
|
160 |
* The username must start with an alphabetical character and must NOT
|
|
|
161 |
* contain any other characters than alphanumerics, the underline and dash.
|
|
|
162 |
*
|
|
|
163 |
* Returns a PEAR_Error if:
|
|
|
164 |
* o user already exists
|
|
|
165 |
* o user or system_user contains illegal characters
|
|
|
166 |
*
|
|
|
167 |
* @throws PEAR_Error
|
|
|
168 |
* @access public
|
|
|
169 |
* @return mixed true on success or PEAR_Error
|
|
|
170 |
* @param string $user the name of the user to add
|
|
|
171 |
* @param string $pass the password of the user tot add
|
|
|
172 |
* @param string $system_user the systemuser this user maps to
|
|
|
173 |
*/
|
|
|
174 |
function addUser($user, $pass, $system_user = '')
|
|
|
175 |
{
|
|
|
176 |
if ($this->userExists($user)) {
|
|
|
177 |
return PEAR::raiseError(
|
|
|
178 |
sprintf(FILE_PASSWD_E_EXISTS_ALREADY_STR, 'User ', $user),
|
|
|
179 |
FILE_PASSWD_E_EXISTS_ALREADY
|
|
|
180 |
);
|
|
|
181 |
}
|
|
|
182 |
if (!preg_match($this->_pcre, $user)) {
|
|
|
183 |
return PEAR::raiseError(
|
|
|
184 |
sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'User ', $user),
|
|
|
185 |
FILE_PASSWD_E_INVALID_CHARS
|
|
|
186 |
);
|
|
|
187 |
}
|
|
|
188 |
@setType($system_user, 'string');
|
|
|
189 |
if (!empty($system_user) && !preg_match($this->_pcre, $system_user)) {
|
|
|
190 |
return PEAR::raiseError(
|
|
|
191 |
sprintf(
|
|
|
192 |
FILE_PASSWD_E_INVALID_CHARS_STR,
|
|
|
193 |
'System user ',
|
|
|
194 |
$system_user
|
|
|
195 |
),
|
|
|
196 |
FILE_PASSWD_E_INVALID_CHARS
|
|
|
197 |
);
|
|
|
198 |
}
|
|
|
199 |
$this->_users[$user]['passwd'] = $this->generatePassword($pass);
|
|
|
200 |
$this->_users[$user]['system'] = $system_user;
|
|
|
201 |
return true;
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
/**
|
|
|
205 |
* Verify the password of a certain user
|
|
|
206 |
*
|
|
|
207 |
* Returns a PEAR_Error if the user doesn't exist.
|
|
|
208 |
*
|
|
|
209 |
* @throws PEAR_Error
|
|
|
210 |
* @access public
|
|
|
211 |
* @return mixed true if passwords equal, false ifthe don't or PEAR_Error
|
|
|
212 |
* @param string $user user whose password should be verified
|
|
|
213 |
* @param string $pass the plaintext password that should be verified
|
|
|
214 |
*/
|
|
|
215 |
function verifyPasswd($user, $pass)
|
|
|
216 |
{
|
|
|
217 |
if (!$this->userExists($user)) {
|
|
|
218 |
return PEAR::raiseError(
|
|
|
219 |
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
|
|
|
220 |
FILE_PASSWD_E_EXISTS_NOT
|
|
|
221 |
);
|
|
|
222 |
}
|
|
|
223 |
$real = $this->_users[$user]['passwd'];
|
|
|
224 |
return ($real === $this->generatePassword($pass, $real));
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
/**
|
|
|
228 |
* Change the password of a certain user
|
|
|
229 |
*
|
|
|
230 |
* Returns a PEAR_Error if user doesn't exist.
|
|
|
231 |
*
|
|
|
232 |
* @throws PEAR_Error
|
|
|
233 |
* @access public
|
|
|
234 |
* @return mixed true on success or PEAR_Error
|
|
|
235 |
*/
|
|
|
236 |
function changePasswd($user, $pass)
|
|
|
237 |
{
|
|
|
238 |
if (!$this->userExists($user)) {
|
|
|
239 |
return PEAR::raiseError(
|
|
|
240 |
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
|
|
|
241 |
FILE_PASSWD_E_EXISTS_NOT
|
|
|
242 |
);
|
|
|
243 |
}
|
|
|
244 |
$this->_users[$user]['passwd'] = $this->generatePassword($pass);
|
|
|
245 |
return true;
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
/**
|
|
|
249 |
* Change the corresponding system user of a certain cvs user
|
|
|
250 |
*
|
|
|
251 |
* Returns a PEAR_Error if:
|
|
|
252 |
* o user doesn't exist
|
|
|
253 |
* o system user contains illegal characters
|
|
|
254 |
*
|
|
|
255 |
* @throws PEAR_Error
|
|
|
256 |
* @access public
|
|
|
257 |
* @return mixed true on success or PEAR_Error
|
|
|
258 |
*/
|
|
|
259 |
function changeSysUser($user, $system)
|
|
|
260 |
{
|
|
|
261 |
if (!$this->userExists($user)) {
|
|
|
262 |
return PEAR::raiseError(
|
|
|
263 |
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
|
|
|
264 |
FILE_PASSWD_E_EXISTS_NOT
|
|
|
265 |
);
|
|
|
266 |
}
|
|
|
267 |
if (!preg_match($this->_pcre, $system)) {
|
|
|
268 |
return PEAR::raiseError(
|
|
|
269 |
sprintf(
|
|
|
270 |
FILE_PASSWD_E_INVALID_CHARS_STR,
|
|
|
271 |
'System user ',
|
|
|
272 |
$system_user
|
|
|
273 |
),
|
|
|
274 |
FILE_PASSWD_E_INVALID_CHARS
|
|
|
275 |
);
|
|
|
276 |
}
|
|
|
277 |
$this->_users[$user]['system'] = $system;
|
|
|
278 |
return true;
|
|
|
279 |
}
|
|
|
280 |
|
|
|
281 |
/**
|
|
|
282 |
* Generate crypted password
|
|
|
283 |
*
|
|
|
284 |
* @static
|
|
|
285 |
* @access public
|
|
|
286 |
* @return string the crypted password
|
|
|
287 |
* @param string $pass new plaintext password
|
|
|
288 |
* @param string $salt new crypted password from which to gain the salt
|
|
|
289 |
*/
|
|
|
290 |
function generatePasswd($pass, $salt = null)
|
|
|
291 |
{
|
|
|
292 |
return File_Passwd::crypt_des($pass, $salt);
|
|
|
293 |
}
|
|
|
294 |
|
|
|
295 |
/**
|
|
|
296 |
* @ignore
|
|
|
297 |
* @deprecated
|
|
|
298 |
*/
|
|
|
299 |
function generatePassword($pass, $salt = null)
|
|
|
300 |
{
|
|
|
301 |
return File_Passwd_Cvs::generatePasswd($pass, $salt);
|
|
|
302 |
}
|
|
|
303 |
}
|
|
|
304 |
?>
|