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 ($uid, $gid, $username, $realname, $shell, $passwd) = @_;
89 $home_dir = $homedir_prefix."/".$username;
91 if($verbose){print("Making a User Account for : $username\n")};
93 # Now let's create the homedir and copy the contents of .../etc/skel into it.
94 my $skel_dir = &forge_get_config ('chroot').'/etc/skel' ;
96 system "cp -r $skel_dir $home_dir";
97 chmod 0755, $home_dir;
99 mkdir $home_dir, 0755;
101 mkdir $home_dir.'/incoming', 0755;
105 push @a, glob "$home_dir/*";
106 chown $uid, $gid, @a;
109 #############################
110 # User Update Function
111 #############################
113 my ($uid, $gid, $username, $realname, $shell, $passwd) = @_;
114 my ($realuid, $realgid);
116 if($verbose){print("Updating Account for: $username\n")};
118 $home_dir = $homedir_prefix.'/'.$username;
119 unless (-d $home_dir.'/incoming') {
120 mkdir $home_dir.'/incoming', 0755;
123 my $realuid=get_file_owner_uid($home_dir);
124 if ($uid eq $realuid){
125 system("chown $uid $home_dir/incoming");
126 system("chmod 0755 $home_dir/incoming");
128 if($verbose){print("Changing owner of $home_dir $realuid -> $uid\n")};
129 system("chown -R $uid $home_dir");
130 system("chmod 0755 $home_dir/incoming");
132 my $realgid=get_file_owner_gid($home_dir);
133 if ($gid eq $realgid){
134 system("chgrp $gid $home_dir/incoming");
136 if($verbose){print("Changing group of $home_dir $realgid -> $gid\n")};
137 system("chgrp -R $gid $home_dir");
141 #############################
142 # User Deletion Function
143 #############################
145 my $username = shift(@_);
147 my $alreadydone=(-f "/var/lib/gforge/tmp/$username.tar.gz");
149 if($verbose){print("Deleting User : $username\n")};
150 if($verbose){print("/bin/mv /var/lib/gforge/chroot/home/users/$username /var/lib/gforge/chroot/home/users/deleted_$username\n")};
151 system("/bin/mv /var/lib/gforge/chroot/home/users/$username /var/lib/gforge/chroot/home/users/deleted_$username");
152 if($verbose){print("/bin/tar -czf /var/lib/gforge/tmp/$username.tar.gz /var/lib/gforge/chroot/home/users/deleted_$username && /bin/rm -rf /var/lib/gforge/chroot/home/users/deleted_$username\n")};
153 system("/bin/tar -czf /var/lib/gforge/tmp/$username.tar.gz /var/lib/gforge/chroot/home/users/deleted_$username && /bin/rm -rf /var/lib/gforge/chroot/home/users/deleted_$username");
157 #############################
158 # User Suspension Function
159 #############################
161 my $this_user = shift(@_);
162 my ($s_username, $s_passwd, $s_date, $s_min, $s_max, $s_inact, $s_expire, $s_flag, $s_resv, $counter);
164 my $new_pass = "!!" . $s_passwd;
167 #############################
169 #############################
170 sub get_file_owner_uid {
171 my $filename = shift(@_);
172 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($filename);
175 #############################
177 #############################
178 sub get_file_owner_gid {
179 my $filename = shift(@_);
180 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($filename);