3 # Debian-specific script to inject local user/group data into the Sourceforge database
4 # Roland Mas <lolando@debian.org>
10 use vars qw/ %passwd %group %shadow
11 %uid2login %gid2groupname
12 %users %groups %user_group
13 $domain_name $datafiles_prefix $organisation_name / ;
18 use vars qw/$dbh @reqlist $query/ ;
19 use vars qw/$sys_default_domain $sys_scm_host
20 $sys_shell_host $sys_users_host $sys_docs_host $sys_lists_host
21 $sys_dns1_host $sys_dns2_host $FTPINCOMING_DIR $FTPFILES_DIR
22 $sys_urlroot $sf_cache_dir $sys_name $sys_themeroot
23 $sys_news_group $sys_dbhost $sys_dbname $sys_dbuser $sys_dbpasswd
24 $sys_ldap_base_dn $sys_ldap_host $admin_login $admin_password
29 require ("/usr/share/gforge/lib/include.pl") ;
35 $datafiles_prefix = "." ;
36 $domain_name = "alioth.debian.org" ;
37 $organisation_name = "Debian" ;
43 open PASSWD, "$datafiles_prefix/passwd" ;
44 open SHADOW, "$datafiles_prefix/shadow" ;
45 open GROUP, "$datafiles_prefix/group" ;
48 my ($login, $name, @list) ;
58 directory => $list[5],
61 ($name, undef) = split /,/, $passwd{$login}{gecos}, 2;
62 $name = '' unless (defined ($name)) ;
63 $passwd{$login}{name} = $name ;
64 $uid2login{$passwd{$login}{uid}} = $login ;
68 my ($groupname, @list) ;
71 $groupname = $list[0] ;
72 $group{$groupname} = {
78 $group{$groupname}{users} = "" if ! defined ($group{$groupname}{users}) ;
79 $gid2groupname{$group{$groupname}{gid}} = $groupname ;
89 lastchangeddate => $list[2],
90 daysbeforechangeable => $list[3],
91 daysbeforemustchange => $list[4],
92 dayswarnbeforeexpire => $list[5],
93 daysafterexpiredisable => $list[6],
94 disableddate => $list[7],
103 # print Dumper \%passwd ;
104 # print Dumper \%group ;
105 # print Dumper \%shadow ;
106 # print Dumper \%uid2login ;
107 # print Dumper \%gid2groupname ;
110 # Process the loaded data
113 foreach my $user (keys %passwd) {
115 $users{$user}{user_id} = $passwd{$user}{uid} ;
116 $users{$user}{user_name} = $user ;
117 $users{$user}{email} = "$user\@debian.org" ;
118 $users{$user}{user_pw} = 'UNKNOWN / OUT OF DATE' ;
119 $users{$user}{realname} = $passwd{$user}{name} ;
120 $users{$user}{shell} = $passwd{$user}{shell} ;
121 $users{$user}{unix_pw} = $shadow{$user}{passwd} ;
122 if ($shadow{$user}{passwd} =~ /^[x!]$/) {
123 $users{$user}{status} = 'N' ;
124 $users{$user}{unix_status} = 'N' ;
126 $users{$user}{status} = 'A' ;
127 $users{$user}{unix_status} = 'A' ;
129 $users{$user}{unix_uid} = $passwd{$user}{uid} ;
130 # $users{$user}{unix_box} = '' ;
131 $users{$user}{add_date} = time() ;
132 # $users{$user}{confirm_hash} = '' ;
133 # $users{$user}{mail_siteupdates} = '' ;
134 # $users{$user}{mail_va} = '' ;
135 # $users{$user}{authorized_keys} = '' ;
136 # $users{$user}{email_new} = '' ;
137 # $users{$user}{people_view_skills} = '' ;
138 # $users{$user}{people_resume} = '' ;
139 # $users{$user}{timezone} = '' ;
140 # $users{$user}{language} = '' ;
141 # $users{$user}{block_ratings} = '' ;
143 $gid = $passwd{$user}{gid} ;
144 $gname = $gid2groupname{$gid} ;
146 $user_group{$gname}{$user} = 1
149 # print "\%users:\n", Dumper \%users ;
151 foreach my $group (keys %group) {
153 $groups{$group}{group_id} = $group{$group}{gid} ;
154 $groups{$group}{group_name} = "$organisation_name group " . $group{$group}{group} ;
155 $groups{$group}{homepage} = $group{$group}{group} . ".$domain_name" ;
156 $groups{$group}{is_public} = 0 ;
157 $groups{$group}{status} = 'A' ;
158 $groups{$group}{unix_group_name} = $group{$group}{group} ;
159 $groups{$group}{unix_box} = "shell";
160 $groups{$group}{http_domain} = $group{$group}{group} . ".$domain_name" ;
161 $groups{$group}{short_description} = "$organisation_name group " . $group{$group}{group} ;
162 $groups{$group}{cvs_box} = "cvs.$domain_name" ;
163 $groups{$group}{license} = "Local group" ;
164 $groups{$group}{register_purpose} = "$organisation_name group " . $group{$group}{group} ;
165 $groups{$group}{license_other} = "" ;
166 $groups{$group}{register_time} = time () ;
167 # $groups{$group}{rand_hash} = "" ;
168 # $groups{$group}{use_mail} = 1 ;
169 # $groups{$group}{use_survey} = 1 ;
170 # $groups{$group}{use_forum} = 1 ;
171 # $groups{$group}{use_pm} = 1 ;
172 # $groups{$group}{use_cvs} = 1 ;
173 # $groups{$group}{use_news} = 1 ;
174 # $groups{$group}{type} = 1 ;
175 # $groups{$group}{use_docman} = 1 ;
176 # $groups{$group}{new_task_address} = "" ;
177 # $groups{$group}{send_all_tasks} = 0 ;
178 # $groups{$group}{use_pm_depend_box} = 1 ;
179 # $groups{$group}{use_ftp} = 1 ;
180 # $groups{$group}{use_tracker} = 1 ;
181 # $groups{$group}{use_frs} = 1 ;
182 # $groups{$group}{use_stats} = 1 ;
183 # $groups{$group}{enable_pserver} = 1 ;
184 # $groups{$group}{enable_anoncvs} = 1 ;
186 @users = split /,/, $group{$group}{users} ;
187 foreach my $user (@users) {
188 $user_group{$group}{$user} = 1 ;
192 # print "\%groups:\n", Dumper \%groups ;
194 # print "\%user_group:\n", Dumper \%user_group ;
197 # Inject the data into the database
201 # debug "Connected to the database OK." ;
203 $dbh->{AutoCommit} = 0;
204 $dbh->{RaiseError} = 1;
208 debug "Starting users injection." ;
214 foreach my $user (keys %users) {
215 $query = "SELECT count(*) FROM users WHERE user_name = '$user'";
217 $sth = $dbh->prepare ($query) ;
219 @array = $sth->fetchrow_array () ;
222 if ($array [0] == 0) {
223 debug "User $user does not exist, creating." ;
224 my $realname = $users{$user}{realname} ;
225 $realname = substr ($realname, 0, 32) ;
226 $realname = $dbh->quote ($realname) ;
227 my $unix_pw = qx(/usr/bin/makepasswd --minchar 8 --maxchar 8) ;
228 $unix_pw = $dbh->quote ($unix_pw) ;
229 $query = "INSERT INTO users (user_name, email,
230 user_pw, realname, status,
231 shell, unix_pw, unix_status,
233 VALUES ('$users{$user}{user_name}',
234 '$users{$user}{email}',
237 '$users{$user}{status}',
238 '$users{$user}{shell}',
239 '$users{$user}{unix_pw}',
240 '$users{$user}{unix_status}',
241 $users{$user}{unix_uid},
242 $users{$user}{add_date})" ;
244 $sth = $dbh->prepare ($query) ;
248 debug "User $user already exists, updating." ;
249 my $realname = $users{$user}{realname} ;
250 $realname = substr ($realname, 0, 32) ;
251 $realname = $dbh->quote ($realname) ;
252 $query = "UPDATE users
253 SET email = '$users{$user}{email}',
254 realname = $realname,
255 status = '$users{$user}{status}',
256 shell = '$users{$user}{shell}',
257 unix_status = '$users{$user}{unix_status}'
258 WHERE user_name = '$users{$user}{user_name}'" ;
260 $sth = $dbh->prepare ($query) ;
266 debug "Committing..." ;
268 debug "Users injection completed correctly." ;
270 debug "Starting groups injection." ;
276 foreach my $group (keys %groups) {
277 $query = "SELECT count(*) FROM groups WHERE unix_group_name = '$group'";
279 $sth = $dbh->prepare ($query) ;
281 @array = $sth->fetchrow_array () ;
284 if ($array [0] == 0) {
285 debug "Group $group does not exist, creating." ;
286 $query = "INSERT INTO groups (group_id, group_name,
287 homepage, is_public, status,
288 unix_group_name, unix_box,
290 short_description, cvs_box,
291 license, register_purpose,
292 license_other, register_time)
293 VALUES ($groups{$group}{group_id},
294 '$groups{$group}{group_name}',
295 '$groups{$group}{homepage}',
296 $groups{$group}{is_public},
297 '$groups{$group}{status}',
298 '$groups{$group}{unix_group_name}',
299 '$groups{$group}{unix_box}',
300 '$groups{$group}{http_domain}',
301 '$groups{$group}{short_description}',
302 '$groups{$group}{cvs_box}',
303 '$groups{$group}{license}',
304 '$groups{$group}{register_purpose}',
305 '$groups{$group}{license_other}',
306 $groups{$group}{register_time})" ;
308 $sth = $dbh->prepare ($query) ;
312 debug "Group $group already exists, nothing to do." ;
317 debug "Committing..." ;
319 debug "Groups injection completed correctly." ;
321 debug "Starting user-group relationships injection." ;
324 # Inject user-groups relationships
327 foreach my $group (keys %user_group) {
328 if (defined ($group{$group}{gid})) {
329 my $gid = $group{$group}{gid} ;
330 foreach my $user (keys %{$user_group{$group}}) {
331 if (defined ($passwd{$user}{uid})) {
332 my $uid = $passwd{$user}{uid} ;
333 $query = "SELECT count(*)
335 WHERE group_id = $gid
336 AND user_id = (SELECT user_id FROM users WHERE unix_uid=$uid)";
338 $sth = $dbh->prepare ($query) ;
340 @array = $sth->fetchrow_array () ;
343 if ($array [0] == 0) {
344 debug "User-group relation $uid - $gid does not exist, creating." ;
345 $query = "INSERT INTO user_group (user_id, group_id)
346 VALUES ((SELECT user_id FROM users WHERE user_name='$users{$user}{user_name}'), $gid)" ;
348 $sth = $dbh->prepare ($query) ;
352 debug "User-group relation $uid - $gid already exists, nothing to do." ;
359 debug "Committing..." ;
361 debug "User-group relationships injection completed correctly." ;
363 # There should be a commit at the end of every block above.
364 # If there is not, then it might be symptomatic of a problem.
365 # For safety, we roll back.
370 warn "Transaction aborted because $@" ;
372 debug "Last SQL query was:\n$query\n(end of query)" ;
374 debug "Please report this bug on the Debian bug-tracking system." ;
375 debug "Please include the previous messages as well to help debugging." ;
376 debug "You should not worry too much about this," ;
377 debug "your DB is still in a consistent state and should be usable." ;
387 print STDERR "$v\n" ;