5 * Script for creating unix users and group permissions that
6 * correspond to the FusionForge users and groups
8 * Novaforge is a registered trade mark from Bull S.A.S
9 * Copyright (C) 2007 Bull S.A.S.
11 * http://novaforge.org/
13 * This file has been developed within the Novaforge(TM) project from Bull S.A.S
14 * and contributed back to GForge community.
16 * FusionForge is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * FusionForge is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License along
27 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
28 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 * This script creates user and group permissions by generating
33 * the /etc/passwd, /etc/shadow and /etc/group files
36 require dirname(__FILE__).'/../www/env.inc.php';
37 require_once $gfcommon.'include/pre.php';
38 require_once $gfcommon.'include/cron_utils.php';
41 $default_shell = "/bin/bash";
43 // Value to add to group id
47 if (file_exists ("/etc/passwd.org") == false)
49 $err .= ", /etc/passwd.org missing!";
51 if (file_exists ("/etc/shadow.org") == false)
53 $err .= ", /etc/shadow.org missing!";
55 if (file_exists ("/etc/group.org") == false)
57 $err .= ", /etc/group.org missing!";
59 if (util_is_root_dir (forge_get_config('homedir_prefix')) == true)
61 $err .= ", homedir_prefix points to root directory!";
67 // Get the users from the database
69 $res = db_query_params ('SELECT user_name,unix_pw,unix_uid,unix_gid,realname,shell FROM users WHERE unix_status=$1',
72 $user_names = &util_result_column_to_array ($res, "user_name");
73 $user_pws = &util_result_column_to_array ($res, "unix_pw");
74 $user_ids = &util_result_column_to_array ($res, "unix_uid");
75 $user_gids = &util_result_column_to_array ($res, "unix_gid");
76 $user_realnames = &util_result_column_to_array ($res, "realname");
77 $user_shells = &util_result_column_to_array ($res, "shell");
79 // Read the "default" users in /etc/passwd.org
81 $h = fopen ("/etc/passwd.org", "r");
82 $passwdcontents = fread ($h, filesize ("/etc/passwd.org"));
84 $passwdlines = explode ("\n", $passwdcontents);
86 // Write the "default" users in /etc/passwd
88 $h2 = fopen ("/etc/passwd", "w");
89 for ($k = 0; $k < count ($passwdlines); $k++)
91 $passwdline = explode (":", $passwdlines [$k]);
92 $def_users [$passwdline [0]] = 1;
93 fwrite ($h2, $passwdlines [$k] . "\n");
96 // Append the users from the database
98 for ($i = 0; $i < count ($user_names); $i++)
100 if ($def_users [$user_names [$i]])
102 // This username was already existing in the /etc/passwd.org file
106 if ((strlen ($user_shells [$i]) > 0) && (file_exists ($user_shells [$i]) == true))
108 $shell = $user_shells [$i];
112 $shell = $default_shell;
114 $line = $user_names [$i] . ":x:" . $user_ids [$i] . ":" . $user_ids [$i] . ":" . $user_realnames [$i] . ":" . forge_get_config('homedir_prefix') . "/" . $user_names [$i] . ":" . $shell . "\n";
122 // Read the "default" users in /etc/shadow.org
124 $h3 = fopen ("/etc/shadow.org", "r");
125 $shadowcontents = fread ($h3, filesize ("/etc/shadow.org"));
127 $shadowlines = explode ("\n", $shadowcontents);
129 // Write the "default" users in /etc/shadow
131 $h4 = fopen("/etc/shadow","w");
132 for($k = 0; $k < count ($shadowlines); $k++)
134 $shadowline = explode (":", $shadowlines [$k]);
135 $def_shadow [$shadowline [0]] = 1;
136 fwrite ($h4, $shadowlines [$k] . "\n");
139 // Append the users from the database
141 for ($i = 0; $i < count ($user_names); $i++)
143 if ($def_shadow [$user_names [$i]])
145 // This username was already existing in the /etc/shadow.org file
149 $line = $user_names [$i] . ":" . $user_pws [$i] . ":12090:0:99999:7:::\n";
156 // Get the groups from the database
158 $res = db_query_params ('SELECT unix_group_name,group_id FROM groups WHERE status=$1 AND use_scm=1',
161 $group_names = &util_result_column_to_array ($res, "unix_group_name");
162 $group_ids = &util_result_column_to_array ($res, "group_id");
164 // Read the "default" groups in /etc/group.org
166 $h5 = fopen ("/etc/group.org", "r");
167 $groupcontents = fread ($h5, filesize ("/etc/group.org"));
169 $grouplines = explode ("\n", $groupcontents);
171 // Write the "default" groups in /etc/group
173 $h6 = fopen ("/etc/group", "w");
174 for ($k = 0; $k < count ($grouplines); $k++)
176 $groupline = explode (":", $grouplines [$k]);
177 $def_group [$groupline [0]] = 1;
178 fwrite ($h6, $grouplines [$k] . "\n");
181 // Add the groups from the database
183 for ($i = 0; $i < count ($group_names); $i++)
185 if ($def_group [$group_names [$i]])
187 // This groupname was already existing in the /etc/group.org file
191 $line = $group_names [$i] . ":x:" . ($group_ids [$i] + $gid_add) . ":";
192 $users = RBACEngine::getInstance()->getUsersByAllowedAction ('scm',$group_ids[$i],'write') ;
193 foreach ($users as $u) {
194 $line .= $u->getUnixName()."," ;
196 $line .= forge_get_config('apache_user') . "\n";
205 $err = "Error" . $err;
207 cron_entry (16, $err);