3 * FusionForge account functions
5 * Copyright 1999-2001, VA Linux Systems, Inc.
6 * Copyright 2010, Franck Villaume - Capgemini
8 * This file is part of FusionForge. FusionForge is free software;
9 * you can redistribute it and/or modify it under the terms of the
10 * GNU General Public License as published by the Free Software
11 * Foundation; either version 2 of the Licence, or (at your option)
14 * FusionForge is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * account_pwvalid() - Validates a password
27 * @param string The plaintext password string
28 * @returns true on success/false on failure
31 function account_pwvalid($pw) {
32 if (strlen($pw) < 6) {
33 $GLOBALS['register_error'] = _('Password must be at least 6 characters.');
40 * account_namevalid() - Validates a login username
42 * @param string The username string
43 * @param bool Check for an unix username
44 * @returns true on success/false on failure
47 function account_namevalid($name, $unix=0) {
50 // If accounts comes from ldap and no shell access, then disable controls.
51 $pluginManager = plugin_manager_get_object();
52 if (!forge_get_config('use_shell') && $pluginManager->PluginIsInstalled('authldap')) {
58 if (strrpos($name,' ') > 0) {
59 $GLOBALS['register_error'] = _('There cannot be any spaces in the login name.');
64 if (strlen($name) < 3) {
65 $GLOBALS['register_error'] = _('Name is too short. It must be at least 3 characters.');
68 if (strlen($name) > 15) {
69 $GLOBALS['register_error'] = _('Name is too long. It must be less than 15 characters.');
73 if (!preg_match('/^[a-z0-9][-a-z0-9_\.]+$/', $name)) {
74 $GLOBALS['register_error'] = _('Illegal character in name.');
79 $regExpReservedNames = "^(root|bin|daemon|adm|lp|sync|shutdown|halt|mail|news|"
80 . "uucp|operator|games|mysql|httpd|nobody|dummy|www|cvs|shell|ftp|irc|"
81 . "debian|ns|download)$";
82 if( preg_match("/$regExpReservedNames/i", $name) ) {
83 $GLOBALS['register_error'] = _('Name is reserved.');
86 if (forge_get_config('use_shell')) {
87 if ( exec("getent passwd $name") != "" ){
88 $GLOBALS['register_error'] = _('That username already exists.');
91 if ( exec("getent group $name") != "" ){
92 $GLOBALS['register_error'] = _('That username already exists.');
96 if (preg_match("/^(anoncvs_)/i",$name)) {
97 $GLOBALS['register_error'] = _('Name is reserved for CVS.');
105 * account_groupnamevalid() - Validates an account group name
107 * @param string The group name string
108 * @returns true on success/false on failure
111 function account_groupnamevalid($name) {
112 if (!account_namevalid($name, 1)) return 0;
115 $regExpReservedGroupNames = "^(www[0-9]?|cvs[0-9]?|shell[0-9]?|ftp[0-9]?|"
116 . "irc[0-9]?|news[0-9]?|mail[0-9]?|ns[0-9]?|download[0-9]?|pub|users|"
117 . "compile|lists|slayer|orbital|tokyojoe|webdev|projects|cvs|monitor|"
119 if(preg_match("/$regExpReservedGroupNames/i",$name)) {
120 $GLOBALS['register_error'] = _('Name is reserved for DNS purposes.');
124 if(preg_match("/_/",$name)) {
125 $GLOBALS['register_error'] = _('Group name cannot contain underscore for DNS reasons.');
133 * genchr() - Generate a random character
135 * This is a local function used for account_salt()
137 * @return int $num A random character
142 $num = util_randnum(46, 122);
143 } while ( ( $num > 57 && $num < 65 ) || ( $num > 90 && $num < 97 ) );
149 * account_gensalt() - A random salt generator
151 * @returns The random salt string
154 function account_gensalt(){
156 // ncommander: modified for cipher selection
157 // crypt() selects the cipher based on
162 switch(forge_get_config('unix_cipher')) {
168 $salt = "$1$" . "$a$b";
173 $salt .= rand(64,126);
183 * account_genunixpw() - Generate unix password
185 * @param string The plaintext password string
186 * @return The encrypted password
189 function account_genunixpw($plainpw) {
190 // ncommander: Support clear password hashing
191 // for usergroup_plain.php
193 if (strcasecmp(forge_get_config('unix_cipher'), 'Plain') == 0) {
196 return crypt($plainpw,account_gensalt());
201 * account_shellselects() - Print out shell selects
203 * @param string The current shell
206 function account_shellselects($current) {
207 $shells = file("/etc/shells");
208 $shells[count($shells)] = "/bin/cvssh";
210 for ($i = 0; $i < count($shells); $i++) {
211 $this_shell = chop($shells[$i]);
213 if ($current == $this_shell) {
214 echo "<option selected=\"selected\" value=$this_shell>$this_shell</option>\n";
216 if (! preg_match("/^#/",$this_shell)){
217 echo "<option value=\"$this_shell\">$this_shell</option>\n";
224 * account_user_homedir() - Returns full path of user home directory
226 * @param string The username
227 * @return home directory path
229 function account_user_homedir($user) {
230 //return '/home/users/'.substr($user,0,1).'/'.substr($user,0,2).'/'.$user;
231 return forge_get_config('homedir_prefix').'/'.$user;
235 * account_group_homedir() - Returns full path of group home directory
237 * @param string The group name
238 * @return home directory path
240 function account_group_homedir($group) {
241 //return '/home/groups/'.substr($group,0,1).'/'.substr($group,0,2).'/'.$group;
242 return forge_get_config('groupdir_prefix').'/'.$group;
247 // c-file-style: "bsd"