3 # dump_database.pl - script to dump data from the database to flat files so the ofher perl
4 # scripts can process it without needing to access the database.
7 require("/usr/share/gforge/lib/include.pl"); # Include all the predefined functions
10 my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) = getpwnam("gforge");
19 # Dump the users Table information
20 my $query = "select unix_uid, unix_gid, unix_status, user_name, shell, unix_pw, realname from users where unix_status != 'N'";
21 my $c = $dbh->prepare($query);
24 while(my ($uid, $gid, $status, $username, $shell, $passwd, $realname) = $c->fetchrow()) {
25 $home_dir = $homedir_prefix.$username;
27 $userlist = "$uid:$gid:$status:$username:$shell:$passwd:$realname\n";
29 push @user_array, $userlist;
35 # Now write out the files
36 write_array_file($file_dir."/dumps/user_dump", @user_array);
37 system("chmod o-r,g-r $file_dir/dumps/user_dump");
39 my $user_file = $file_dir . "/dumps/user_dump";
40 my ($uid, $gid, $status, $username, $shell, $passwd, $realname);
42 # Open up all the files that we need.
43 @userdump_array = open_array_file($user_file);
46 # Loop through @userdump_array and deal w/ users.
48 if($verbose){print ("\n\n Processing Users\n\n")};
49 while ($ln = pop(@userdump_array)) {
51 ($uid, $gid, $status, $username, $shell, $passwd, $realname) = split(":", $ln);
52 $username =~ tr/A-Z/a-z/;
53 $user_exists = (-d $homedir_prefix .'/'. $username || -f "/var/lib/gforge/tmp/$username.tar.gz");
55 if ($status eq 'A' && $user_exists) {
56 update_user($uid, $gid, $username, $realname, $shell, $passwd);
58 } elsif ($status eq 'A' && !$user_exists) {
59 add_user($uid, $gid, $username, $realname, $shell, $passwd);
61 } elsif ($status eq 'D' && $user_exists) {
62 delete_user($username);
64 } elsif ($status eq 'D' && !$user_exists) {
65 if($verbose){print("Error trying to delete user: $username\n")};
67 } elsif ($status eq 'S' && $user_exists) {
68 suspend_user($username);
70 } elsif ($status eq 'S' && !$user_exists) {
71 if($verbose){print("Error trying to suspend user: $username\n")};
74 if($verbose){print("Unknown Status Flag: $username\n")};
78 ###############################################
80 ###############################################
82 #############################
84 #############################
86 my $thecmd = shift(@_);
91 return system($thecmd);
94 #############################
96 #############################
98 my ($uid, $gid, $username, $realname, $shell, $passwd) = @_;
101 $home_dir = $homedir_prefix."/".$username;
103 if($verbose){print("Making a User Account for : $username\n")};
105 # Now let's create the homedir and copy the contents of .../etc/skel into it.
106 my $skel_dir = &forge_get_config ('chroot').'/etc/skel' ;
108 system "cp -r $skel_dir $home_dir";
109 chmod 0755, $home_dir;
111 mkdir $home_dir, 0755;
113 mkdir $home_dir.'/incoming', 0755;
117 push @a, glob "$home_dir/*";
118 chown $uid, $gid, @a;
121 #############################
122 # User Update Function
123 #############################
125 my ($uid, $gid, $username, $realname, $shell, $passwd) = @_;
126 my ($realuid, $realgid);
128 if($verbose){print("Updating Account for: $username\n")};
130 $home_dir = $homedir_prefix.'/'.$username;
131 unless (-d $home_dir.'/incoming') {
132 mkdir $home_dir.'/incoming', 0755;
135 my $realuid=get_file_owner_uid($home_dir);
136 if ($uid eq $realuid){
137 system("chown $uid $home_dir/incoming");
138 system("chmod 0755 $home_dir/incoming");
140 if($verbose){print("Changing owner of $home_dir $realuid -> $uid\n")};
141 system("chown -R $uid $home_dir");
142 system("chmod 0755 $home_dir/incoming");
144 my $realgid=get_file_owner_gid($home_dir);
145 if ($gid eq $realgid){
146 system("chgrp $gid $home_dir/incoming");
148 if($verbose){print("Changing group of $home_dir $realgid -> $gid\n")};
149 system("chgrp -R $gid $home_dir");
153 #############################
154 # User Deletion Function
155 #############################
157 my $username = shift(@_);
159 my $alreadydone=(-f "/var/lib/gforge/tmp/$username.tar.gz");
161 my $oldmask = umask(077);
163 print("Deleting User : $username\n");
165 run_verbose("/bin/mv /var/lib/gforge/chroot/home/users/$username /var/lib/gforge/chroot/home/users/deleted_$username");
166 run_verbose("cd / && /bin/tar -cf - /var/lib/gforge/chroot/home/users/deleted_$username | /bin/gzip -n9 >/var/lib/gforge/tmp/$username.tar.gz && /bin/rm -rf /var/lib/gforge/chroot/home/users/deleted_$username");
171 #############################
172 # User Suspension Function
173 #############################
175 my $this_user = shift(@_);
176 my ($s_username, $s_passwd, $s_date, $s_min, $s_max, $s_inact, $s_expire, $s_flag, $s_resv, $counter);
178 my $new_pass = "!!" . $s_passwd;
181 #############################
183 #############################
184 sub get_file_owner_uid {
185 my $filename = shift(@_);
186 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($filename);
189 #############################
191 #############################
192 sub get_file_owner_gid {
193 my $filename = shift(@_);
194 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($filename);