home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / File / Passwd / Unix.php < prev   
Encoding:
PHP Script  |  2008-07-02  |  18.4 KB  |  660 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * File::Passwd::Unix
  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: Unix.php,v 1.17 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 standard Unix passwd files.
  31. * <kbd><u>Usage Example:</u></kbd>
  32. * <code>
  33. *   $passwd = &File_Passwd::factory('Unix');
  34. *   $passwd->setFile('/my/passwd/file');
  35. *   $passwd->load();
  36. *   $passwd->addUser('mike', 'secret');
  37. *   $passwd->save();
  38. * </code>
  39. * <kbd><u>Output of listUser()</u></kbd>
  40. * # using the 'name map':
  41. * <pre>
  42. *      array
  43. *       + user  => array
  44. *                   + pass  => crypted_passwd or 'x' if shadowed
  45. *                   + uid   => user id
  46. *                   + gid   => group id
  47. *                   + gecos => comments
  48. *                   + home  => home directory
  49. *                   + shell => standard shell
  50. * </pre>
  51. * # without 'name map':
  52. * <pre>
  53. *      array
  54. *       + user  => array
  55. *                   + 0  => crypted_passwd
  56. *                   + 1  => ...
  57. *                   + 2  => ...
  58. * </pre>
  59. * @author   Michael Wallner <mike@php.net>
  60. * @package  File_Passwd
  61. * @version  $Revision: 1.17 $
  62. * @access   public
  63. */
  64. class File_Passwd_Unix extends File_Passwd_Common
  65. {
  66.     /**
  67.     * A 'name map' wich refer to the extra properties
  68.     *
  69.     * @var array
  70.     * @access private
  71.     */
  72.     var $_map = array('uid', 'gid', 'gecos', 'home', 'shell');
  73.     
  74.     /**
  75.     * Whether to use the 'name map' or not
  76.     *
  77.     * @var boolean
  78.     * @access private
  79.     */
  80.     var $_usemap = true;
  81.     
  82.     /**
  83.     * Whether the passwords of this passwd file are shadowed in another file
  84.     *
  85.     * @var boolean
  86.     * @access private
  87.     */
  88.     var $_shadowed = false;
  89.     
  90.     /**
  91.     * Encryption mode, either md5 or des
  92.     *
  93.     * @var string
  94.     * @access private
  95.     */
  96.     var $_mode = 'des';
  97.     
  98.     /**
  99.     * Supported encryption modes
  100.     * 
  101.     * @var array
  102.     * @access private
  103.     */
  104.     var $_modes = array('md5' => 'md5', 'des' => 'des');
  105.     
  106.     /**
  107.     * Constructor
  108.     *
  109.     * @access public
  110.     * @param  string    $file   path to passwd file
  111.     */
  112.     function File_Passwd_Unix($file = 'passwd')
  113.     {
  114.         parent::__construct($file);
  115.     }
  116.     
  117.     /**
  118.     * Fast authentication of a certain user
  119.     * 
  120.     * Returns a PEAR_Error if:
  121.     *   o file doesn't exist
  122.     *   o file couldn't be opened in read mode
  123.     *   o file couldn't be locked exclusively
  124.     *   o file couldn't be unlocked (only if auth fails)
  125.     *   o file couldn't be closed (only if auth fails)
  126.     *   o invalid encryption mode <var>$mode</var> was provided
  127.     *
  128.     * @static   call this method statically for a reasonable fast authentication
  129.     * @access   public
  130.     * @return   mixed   true if authenticated, false if not or PEAR_Error
  131.     * @param    string  $file   path to passwd file
  132.     * @param    string  $user   user to authenticate
  133.     * @param    string  $pass   plaintext password
  134.     * @param    string  $mode   encryption mode to use (des or md5)
  135.     */
  136.     function staticAuth($file, $user, $pass, $mode)
  137.     {
  138.         $line = File_Passwd_Common::_auth($file, $user);
  139.         if (!$line || PEAR::isError($line)) {
  140.             return $line;
  141.         }
  142.         list(,$real)= explode(':', $line);
  143.         $crypted    = File_Passwd_Unix::_genPass($pass, $real, $mode);
  144.         if (PEAR::isError($crypted)) {
  145.             return $crypted;
  146.         }
  147.         return ($crypted === $real);
  148.     }
  149.     
  150.     /**
  151.     * Apply changes an rewrite passwd file
  152.     *
  153.     * Returns a PEAR_Error if:
  154.     *   o directory in which the file should reside couldn't be created
  155.     *   o file couldn't be opened in write mode
  156.     *   o file couldn't be locked exclusively
  157.     *   o file couldn't be unlocked
  158.     *   o file couldn't be closed
  159.     * 
  160.     * @throws PEAR_Error
  161.     * @access public
  162.     * @return mixed true on success or PEAR_Error
  163.     */
  164.     function save()
  165.     {
  166.         $content = '';
  167.         foreach ($this->_users as $user => $array){
  168.             $pass   = array_shift($array);
  169.             $extra  = implode(':', $array);
  170.             $content .= $user . ':' . $pass;
  171.             if (!empty($extra)) {
  172.                 $content .= ':' . $extra;
  173.             }
  174.             $content .= "\n";
  175.         }
  176.         return $this->_save($content);
  177.     }
  178.     
  179.     /**
  180.     * Parse the Unix password file
  181.     *
  182.     * Returns a PEAR_Error if passwd file has invalid format.
  183.     * 
  184.     * @throws PEAR_Error
  185.     * @access public
  186.     * @return mixed true on success or PEAR_Error
  187.     */
  188.     function parse()
  189.     {
  190.         $this->_users = array();
  191.         foreach ($this->_contents as $line){
  192.             $parts = explode(':', $line);
  193.             if (count($parts) < 2) {
  194.                 return PEAR::raiseError(
  195.                     FILE_PASSWD_E_INVALID_FORMAT_STR,
  196.                     FILE_PASSWD_E_INVALID_FORMAT
  197.                 );
  198.             }
  199.             $user = array_shift($parts);
  200.             $pass = array_shift($parts);
  201.             if ($pass == 'x') {
  202.                 $this->_shadowed = true;
  203.             }
  204.             $values = array();
  205.             if ($this->_usemap) {
  206.                 $values['pass'] = $pass;
  207.                 foreach ($parts as $i => $value){
  208.                     if (isset($this->_map[$i])) {
  209.                         $values[$this->_map[$i]] = $value;
  210.                     } else {
  211.                         $values[$i+1] = $value;
  212.                     }
  213.                 }
  214.             } else {
  215.                 $values = array_merge(array($pass), $parts);
  216.             }
  217.             $this->_users[$user] = $values;
  218.             
  219.         }
  220.         $this->_contents = array();
  221.         return true;
  222.     }
  223.     
  224.     /**
  225.     * Set the encryption mode
  226.     * 
  227.     * Supported encryption modes are des and md5.
  228.     * 
  229.     * Returns a PEAR_Error if supplied encryption mode is not supported.
  230.     *
  231.     * @see      setMode()
  232.     * @see      listModes()
  233.     * 
  234.     * @throws   PEAR_Error
  235.     * @access   public
  236.     * @return   mixed   true on succes or PEAR_Error
  237.     * @param    string  $mode   encryption mode to use; either md5 or des
  238.     */
  239.     function setMode($mode)
  240.     {
  241.         $mode = strToLower($mode);
  242.         if (!isset($this->_modes[$mode])) {
  243.             return PEAR::raiseError(
  244.                 sprintf(FILE_PASSWD_E_INVALID_ENC_MODE_STR, $mode),
  245.                 FILE_PASSWD_E_INVALID_ENC_MODE
  246.             );
  247.         }
  248.         $this->_mode = $mode;
  249.         return true;
  250.     }
  251.     
  252.     /** 
  253.     * Get supported encryption modes
  254.     *
  255.     * <pre>
  256.     *   array
  257.     *    + md5
  258.     *    + des
  259.     * </pre>
  260.     * 
  261.     * @see      setMode()
  262.     * @see      getMode()
  263.     * 
  264.     * @access   public
  265.     * @return   array
  266.     */
  267.     function listModes()
  268.     {
  269.         return $this->_modes;
  270.     }
  271.  
  272.     /**
  273.     * Get actual encryption mode
  274.     *
  275.     * @see      listModes()
  276.     * @see      setMode()
  277.     * 
  278.     * @access   public
  279.     * @return   string
  280.     */
  281.     function getMode()
  282.     {
  283.         return $this->_mode;
  284.     }
  285.     
  286.     /**
  287.     * Whether to use the 'name map' of the extra properties or not
  288.     * 
  289.     * Default Unix passwd files look like:
  290.     * <pre>
  291.     * user:password:user_id:group_id:gecos:home_dir:shell
  292.     * </pre>
  293.     * 
  294.     * The default 'name map' for properties except user and password looks like:
  295.     *   o uid
  296.     *   o gid
  297.     *   o gecos
  298.     *   o home
  299.     *   o shell
  300.     * 
  301.     * If you want to change the naming of the standard map use 
  302.     * File_Passwd_Unix::setMap(array()).
  303.     *
  304.     * @see      setMap()
  305.     * @see      getMap()
  306.     * 
  307.     * @access   public
  308.     * @return   boolean always true if you set a value (true/false) OR
  309.     *                   the actual value if called without param
  310.     * 
  311.     * @param    boolean $bool   whether to use the 'name map' or not
  312.     */
  313.     function useMap($bool = null)
  314.     {
  315.         if (is_null($bool)) {
  316.             return $this->_usemap;
  317.         }
  318.         $this->_usemap = (bool) $bool;
  319.         return true;
  320.     }
  321.     
  322.     /**
  323.     * Set the 'name map' to use with the extra properties of the user
  324.     * 
  325.     * This map is used for naming the associative array of the extra properties.
  326.     *
  327.     * Returns a PEAR_Error if <var>$map</var> was not of type array.
  328.     * 
  329.     * @see      getMap()
  330.     * @see      useMap()
  331.     * 
  332.     * @throws   PEAR_Error
  333.     * @access   public
  334.     * @return   mixed       true on success or PEAR_Error
  335.     */
  336.     function setMap($map = array())
  337.     {
  338.         if (!is_array($map)) {
  339.             return PEAR::raiseError(
  340.                 sprintf(FILE_PASSWD_E_PARAM_MUST_BE_ARRAY_STR, '$map'),
  341.                 FILE_PASSWD_E_PARAM_MUST_BE_ARRAY
  342.             );
  343.         }
  344.         $this->_map = $map;
  345.         return true;
  346.     }
  347.     
  348.     /**
  349.     * Get the 'name map' which is used for the extra properties of the user
  350.     *
  351.     * @see      setMap()
  352.     * @see      useMap()
  353.     * 
  354.     * @access public
  355.     * @return array
  356.     */
  357.     function getMap()
  358.     {
  359.         return $this->_map;
  360.     }
  361.     
  362.     /**
  363.     * If the passwords of this passwd file are shadowed in another file.
  364.     *
  365.     * @access public
  366.     * @return boolean
  367.     */
  368.     function isShadowed()
  369.     {
  370.         return $this->_shadowed;
  371.     }
  372.     
  373.     /**
  374.     * Add an user
  375.     *
  376.     * The username must start with an alphabetical character and must NOT
  377.     * contain any other characters than alphanumerics, the underline and dash.
  378.     * 
  379.     * If you use the 'name map' you should also use these naming in
  380.     * the supplied extra array, because your values would get mixed up
  381.     * if they are in the wrong order, which is always true if you
  382.     * DON'T use the 'name map'!
  383.     * 
  384.     * So be warned and USE the 'name map'!
  385.     * 
  386.     * If the passwd file is shadowed, the user will be added though, but
  387.     * with an 'x' as password, and a PEAR_Error will be returned, too.
  388.     * 
  389.     * Returns a PEAR_Error if:
  390.     *   o user already exists
  391.     *   o user contains illegal characters
  392.     *   o encryption mode is not supported
  393.     *   o passwords are shadowed in another file
  394.     *   o any element of the <var>$extra</var> array contains a colon (':')
  395.     * 
  396.     * @throws PEAR_Error
  397.     * @access public
  398.     * @return mixed true on success or PEAR_Error
  399.     * @param  string    $user   the name of the user to add
  400.     * @param  string    $pass   the password of the user to add
  401.     * @param  array     $extra  extra properties of user to add
  402.     */
  403.     function addUser($user, $pass, $extra = array())
  404.     {
  405.         if ($this->userExists($user)) {
  406.             return PEAR::raiseError(
  407.                 sprintf(FILE_PASSWD_E_EXISTS_ALREADY_STR, 'User ', $user),
  408.                 FILE_PASSWD_E_EXISTS_ALREADY
  409.             );
  410.         }
  411.         if (!preg_match($this->_pcre, $user)) {
  412.             return PEAR::raiseError(
  413.                 sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'User ', $user),
  414.                 FILE_PASSWD_E_INVALID_CHARS
  415.             );
  416.         }
  417.         if (!is_array($extra)) {
  418.             setType($extra, 'array');
  419.         }
  420.         foreach ($extra as $e){
  421.             if (strstr($e, ':')) {
  422.             return PEAR::raiseError(
  423.                 sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'Property ', $e),
  424.                 FILE_PASSWD_E_INVALID_CHARS
  425.             );
  426.             }
  427.         }
  428.         
  429.         /**
  430.         * If passwords of the passwd file are shadowed, 
  431.         * the password of the user will be set to 'x'.
  432.         */
  433.         if ($this->_shadowed) {
  434.             $pass = 'x';
  435.         } else {
  436.             $pass = $this->_genPass($pass);
  437.             if (PEAR::isError($pass)) {
  438.                 return $pass;
  439.             }
  440.         }
  441.         
  442.         /**
  443.         * If you don't use the 'name map' the user array will be numeric.
  444.         */
  445.         if (!$this->_usemap) {
  446.             array_unshift($extra, $pass);
  447.             $this->_users[$user] = $extra;
  448.         } else {
  449.             $map = $this->_map;
  450.             array_unshift($map, 'pass');
  451.             $extra['pass'] = $pass;
  452.             foreach ($map as $key){
  453.                 $this->_users[$user][$key] = @$extra[$key];
  454.             }
  455.         }
  456.         
  457.         /**
  458.         * Raise a PEAR_Error if passwords are shadowed.
  459.         */
  460.         if ($this->_shadowed) {
  461.             return PEAR::raiseError(
  462.                 'Password has been set to \'x\' because they are '.
  463.                 'shadowed in another file.', 0
  464.             );
  465.         }
  466.         return true;
  467.     }
  468.     
  469.     /**
  470.     * Modify properties of a certain user
  471.     *
  472.     * # DON'T MODIFY THE PASSWORD WITH THIS METHOD!
  473.     * 
  474.     * You should use this method only if the 'name map' is used, too.
  475.     * 
  476.     * Returns a PEAR_Error if:
  477.     *   o user doesn't exist
  478.     *   o any property contains a colon (':')
  479.     * 
  480.     * @see      changePasswd()
  481.     * 
  482.     * @throws   PEAR_Error
  483.     * @access   public
  484.     * @return   mixed       true on success or PEAR_Error
  485.     * @param    string      $user           the user to modify
  486.     * @param    array       $properties     an associative array of 
  487.     *                                       properties to modify
  488.     */
  489.     function modUser($user, $properties = array())
  490.     {
  491.         if (!$this->userExists($user)) {
  492.             return PEAR::raiseError(
  493.                 sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
  494.                 FILE_PASSWD_E_EXISTS_NOT
  495.             );
  496.         }
  497.         
  498.         if (!is_array($properties)) {
  499.             setType($properties, 'array');
  500.         }
  501.         
  502.         foreach ($properties as $key => $value){
  503.             if (strstr($value, ':')) {
  504.                 return PEAR::raiseError(
  505.                     sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'User ', $user),
  506.                     FILE_PASSWD_E_INVALID_CHARS
  507.                 );
  508.             }
  509.             $this->_users[$user][$key] = $value;
  510.         }
  511.         
  512.         return true;
  513.     }
  514.     
  515.     /**
  516.     * Change the password of a certain user
  517.     *
  518.     * Returns a PEAR_Error if:
  519.     *   o user doesn't exists
  520.     *   o passwords are shadowed in another file
  521.     *   o encryption mode is not supported
  522.     * 
  523.     * @throws PEAR_Error
  524.     * @access public
  525.     * @return mixed true on success or PEAR_Error
  526.     * @param string $user   the user whose password should be changed
  527.     * @param string $pass   the new plaintext password
  528.     */
  529.     function changePasswd($user, $pass)
  530.     {
  531.         if ($this->_shadowed) {
  532.             return PEAR::raiseError(
  533.                 'Passwords of this passwd file are shadowed.', 
  534.                 0
  535.             );
  536.         }
  537.         
  538.         if (!$this->userExists($user)) {
  539.             return PEAR::raiseError(
  540.                 sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
  541.                 FILE_PASSWD_E_EXISTS_NOT
  542.             );
  543.         }
  544.         
  545.         $pass = $this->_genPass($pass);
  546.         if (PEAR::isError($pass)) {
  547.             return $pass;
  548.         }
  549.         
  550.         if ($this->_usemap) {
  551.             $this->_users[$user]['pass'] = $pass;
  552.         } else {
  553.             $this->_users[$user][0] = $pass;
  554.         }
  555.         
  556.         return true;
  557.     }
  558.     
  559.     /**
  560.     * Verify the password of a certain user
  561.     * 
  562.     * Returns a PEAR_Error if:
  563.     *   o user doesn't exist
  564.     *   o encryption mode is not supported
  565.     *
  566.     * @throws PEAR_Error
  567.     * @access public
  568.     * @return mixed true if passwors equal, false if they don't or PEAR_Error
  569.     * @param  string    $user   the user whose password should be verified
  570.     * @param  string    $pass   the password to verify
  571.     */
  572.     function verifyPasswd($user, $pass)
  573.     {
  574.         if (!$this->userExists($user)) {
  575.             return PEAR::raiseError(
  576.                 sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
  577.                 FILE_PASSWD_E_EXISTS_NOT
  578.             );
  579.         }
  580.         $real = 
  581.             $this->_usemap ? 
  582.             $this->_users[$user]['pass'] : 
  583.             $this->_users[$user][0]
  584.         ;
  585.         return ($real === $this->_genPass($pass, $real));
  586.     }
  587.     
  588.     /**
  589.     * Generate crypted password from the plaintext password
  590.     *
  591.     * Returns a PEAR_Error if actual encryption mode is not supported.
  592.     * 
  593.     * @throws PEAR_Error
  594.     * @access private
  595.     * @return mixed     the crypted password or PEAR_Error
  596.     * @param  string    $pass   the plaintext password
  597.     * @param  string    $salt   the crypted password from which to gain the salt
  598.     * @param  string    $mode   the encryption mode to use; don't set, because
  599.     *                           it's usually taken from File_Passwd_Unix::_mode
  600.     */
  601.     function _genPass($pass, $salt = null, $mode = null)
  602.     {
  603.         static $crypters;
  604.         if (!isset($crypters)) {
  605.             $crypters = get_class_methods('File_Passwd');
  606.         }
  607.         
  608.         $mode = !isset($mode) ? strToLower($this->_mode) : strToLower($mode);
  609.         $func = 'crypt_' . $mode;
  610.         
  611.         if (!in_array($func, $crypters)) {
  612.             return PEAR::raiseError(
  613.                 sprintf(FILE_PASSWD_E_INVALID_ENC_MODE_STR, $mode),
  614.                 FILE_PASSWD_E_INVALID_ENC_MODE
  615.             );
  616.         }
  617.         
  618.         return call_user_func(array('File_Passwd', $func), $pass, $salt);
  619.     }
  620.     
  621.     /**
  622.     * Generate Password
  623.     *
  624.     * Returns PEAR_Error FILE_PASSD_E_INVALID_ENC_MODE if the supplied
  625.     * encryption mode is not supported.
  626.     *
  627.     * @see File_Passwd
  628.     * @static
  629.     * @access   public
  630.     * @return   mixed   The crypted password on success or PEAR_Error on failure.
  631.     * @param    string  $pass The plaintext password.
  632.     * @param    string  $mode The encryption mode to use.
  633.     * @param    string  $salt The salt to use.
  634.     */
  635.     function generatePasswd($pass, $mode = FILE_PASSWD_MD5, $salt = null)
  636.     {
  637.         if (!isset($mode)) {
  638.             return PEAR::raiseError(
  639.                 sprintf(FILE_PASSWD_E_INVALID_ENC_MODE_STR, '<NULL>'),
  640.                 FILE_PASSWD_E_INVALID_ENC_MODE                
  641.             );
  642.         }
  643.         return File_Passwd_Unix::_genPass($pass, $salt, $mode);
  644.     }
  645.     
  646.     /**
  647.      * @ignore
  648.      * @deprecated
  649.      */
  650.     function generatePassword($pass, $mode = FILE_PASSWD_MD5, $salt = null)
  651.     {
  652.         return File_Passwd_Unix::generatePasswd($pass, $mode, $salt);
  653.     }
  654.     
  655. }
  656. ?>