2 /* vim: set ts=4 sw=4: */
\r
3 // +----------------------------------------------------------------------+
\r
5 // +----------------------------------------------------------------------+
\r
6 // | Copyright (c) 1997-2002 The PHP Group |
\r
7 // +----------------------------------------------------------------------+
\r
8 // | This source file is subject to version 2.0 of the PHP license, |
\r
9 // | that is bundled with this package in the file LICENSE, and is |
\r
10 // | available at through the world-wide-web at |
\r
11 // | http://www.php.net/license/2_02.txt. |
\r
12 // | If you did not receive a copy of the PHP license and are unable to |
\r
13 // | obtain it through the world-wide-web, please send a note to |
\r
14 // | license@php.net so we can mail you a copy immediately. |
\r
15 // +----------------------------------------------------------------------+
\r
16 // | Author: Rasmus Lerdorf <rasmus@php.net> |
\r
17 // +----------------------------------------------------------------------+
\r
19 // $Id: File_Passwd.php,v 1.1 2004-01-25 03:57:15 rurban Exp $
\r
21 // Manipulate standard UNIX passwd,.htpasswd and CVS pserver passwd files
\r
23 require_once 'PEAR.php' ;
\r
26 * Class to manage passwd-style files
\r
28 * @author Rasmus Lerdorf <rasmus@php.net>
\r
39 * Hash list of users
\r
45 * hash list of csv-users
\r
51 * filehandle for lockfile
\r
63 * name of the lockfile
\r
66 var $lockfile = './passwd.lock';
\r
70 * Requires the name of the passwd file. This functions opens the file and read it.
\r
71 * Changes to this file will written first in the lock file, so it is still possible
\r
72 * to access the passwd file by another programs. The lock parameter controls the locking
\r
73 * oft the lockfile, not of the passwd file! ( Swapping $lock and $lockfile would
\r
74 * breaks bc to v1.3 and smaller).
\r
75 * Don't forget to call close() to save changes!
\r
77 * @param $file name of the passwd file
\r
78 * @param $lock if 'true' $lockfile will be locked
\r
79 * @param $lockfile name of the temp file, where changes are saved
\r
85 function File_Passwd($file, $lock = 0, $lockfile = "") {
\r
86 $this->filename = $file;
\r
87 if( !empty( $lockfile) ) {
\r
88 $this->lockfile = $lockfile ;
\r
92 $this->fplock = fopen($this->lockfile, 'w');
\r
93 flock($this->fplock, LOCK_EX);
\r
94 $this->locked = true;
\r
97 $fp = fopen($file,'r') ;
\r
99 return new PEAR_Error( "Couldn't open '$file'!", 1, PEAR_ERROR_RETURN) ;
\r
101 while(!feof($fp)) {
\r
102 $line = fgets($fp, 128);
\r
103 list($user, $pass, $cvsuser) = explode(':', $line);
\r
104 if(strlen($user)) {
\r
105 $this->users[$user] = trim($pass);
\r
106 $this->cvs[$user] = trim($cvsuser);
\r
110 } // end func File_Passwd()
\r
115 * @param $user new user id
\r
116 * @param $pass password for new user
\r
117 * @param $cvs cvs user id (needed for pserver passwd files)
\r
119 * @return mixed returns PEAR_Error, if the user already exists
\r
122 function addUser($user, $pass, $cvsuser = "") {
\r
123 if(!isset($this->users[$user]) && $this->locked) {
\r
124 $this->users[$user] = crypt($pass);
\r
125 $this->cvs[$user] = $cvsuser;
\r
128 return new PEAR_Error( "Couldn't add user '$user', because the user already exists!", 2, PEAR_ERROR_RETURN);
\r
130 } // end func addUser()
\r
135 * @param $user user id
\r
136 * @param $pass new password for user
\r
137 * @param $cvs cvs user id (needed for pserver passwd files)
\r
139 * @return mixed returns PEAR_Error, if the user doesn't exists
\r
143 function modUser($user, $pass, $cvsuser="") {
\r
144 if(isset($this->users[$user]) && $this->locked) {
\r
145 $this->users[$user] = crypt($pass);
\r
146 $this->cvs[$user] = $cvsuser;
\r
149 return new PEAR_Error( "Couldn't modify user '$user', because the user doesn't exists!", 3, PEAR_ERROR_RETURN) ;
\r
151 } // end func modUser()
\r
156 * @param $user user id
\r
158 * @return mixed returns PEAR_Error, if the user doesn't exists
\r
162 function delUser($user) {
\r
163 if(isset($this->users[$user]) && $this->locked) {
\r
164 unset($this->users[$user]);
\r
165 unset($this->cvs[$user]);
\r
167 return new PEAR_Error( "Couldn't delete user '$user', because the user doesn't exists!", 3, PEAR_ERROR_RETURN) ;
\r
169 } // end func delUser()
\r
172 * Verifies a user's password
\r
174 * @param $user user id
\r
175 * @param $pass password for user
\r
177 * @return boolean true if password is ok
\r
180 function verifyPassword($user, $pass) {
\r
181 if(isset($this->users[$user])) {
\r
182 if($this->users[$user] == crypt($pass, substr($this->users[$user], 0, 2))) return true;
\r
185 } // end func verifyPassword()
\r
188 * Return all users from passwd file
\r
193 function listUsers() {
\r
194 return $this->users;
\r
195 } // end func listUsers()
\r
198 * Writes changes to passwd file and unlocks it
\r
203 if($this->locked) {
\r
204 foreach($this->users as $user => $pass) {
\r
205 if($this->cvs[$user]) {
\r
206 fputs($this->fplock, "$user:$pass:" . $this->cvs[$user] . "\n");
\r
208 fputs($this->fplock, "$user:$pass\n");
\r
211 rename($this->lockfile, $this->filename);
\r
212 flock($this->fplock, LOCK_UN);
\r
213 $this->locked = false;
\r
214 fclose($this->fplock);
\r
216 } // end func close()
\r