Code

Updated password field handling
[gosa.git] / gosa-core / include / password-methods / class_password-methods-crypt.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id$$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
23 class passwordMethodCrypt extends passwordMethod
24 {
25   function passwordMethodCrypt($config)
26   {
27   }
30   function is_available()
31   {
32     if(function_exists("crypt")){
33       return true;
34     }else{
35       return false;
36     }
37   }
39   function create_template_hash($attrs)
40   {
41     return($this->generate_hash('N0T$3T4N0W').'N0T$3T4N0W');
42   }
45   function generate_hash($pwd)
46   {
47     if ($this->hash == "crypt/standard-des"){
48       $salt = "";
49       for ($i = 0; $i < 2; $i++) {
50           $salt .= get_random_char();
51       }
52     }
54     if ($this->hash == "crypt/enhanced-des"){
55       $salt = "_";
56       for ($i = 0; $i < 8; $i++) {
57           $salt .= get_random_char();
58       }
59     }
61     if ($this->hash == "crypt/md5"){
62       $salt = "\$1\$";
63       for ($i = 0; $i < 8; $i++) {
64           $salt .= get_random_char();
65       }
66       $salt .= "\$";
67     }
69     if ($this->hash == "crypt/blowfish"){
70       $salt = "\$2a\$07\$";
71       for ($i = 0; $i < CRYPT_SALT_LENGTH; $i++) {
72           $salt .= get_random_char();
73       }
74       $salt .= "\$";
75     }
77     return "{CRYPT}".crypt($pwd, $salt);
78   }
81   function get_hash_name()
82   {
83     $hashes= array();
84     if (CRYPT_STD_DES == 1) {
85       $hashes[]= "crypt/standard-des";
86     }
88     if (CRYPT_EXT_DES == 1) {
89       $hashes[]= "crypt/enhanced-des";
90     }
92     if (CRYPT_MD5 == 1) {
93       $hashes[]= "crypt/md5";
94     }
96     if (CRYPT_BLOWFISH == 1) {
97       $hashes[]= "crypt/blowfish";
98     }
100     return $hashes;
101   }
104   function _extract_method($password_hash)
105   {
106     if (!preg_match('/^{crypt}/i', $password_hash)){
107       return "";
108     }
110     $password_hash= preg_replace('/^{[^}]+}!?/', '', $password_hash);
112     if (preg_match("/^[a-zA-Z0-9.\/][a-zA-Z0-9.\/]/", $password_hash)){
113       return "crypt/standard-des";
114     }
116     if (preg_match("/^_[a-zA-Z0-9.\/]/", $password_hash)){
117       return "crypt/enhanced-des";
118     }
119     
120     if (preg_match('/^\$1\$/', $password_hash)){
121       return "crypt/md5";
122     }
124     if (preg_match('/^(\$2\$|\$2a\$)/', $password_hash)){
125       return "crypt/blowfish";
126     }
128     return "";
129   }
133 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
134 ?>