/
IcculusFinger_planmove.pl
executable file
·128 lines (103 loc) · 4.04 KB
/
IcculusFinger_planmove.pl
OlderNewer
1
#!/usr/bin/perl -w
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#-----------------------------------------------------------------------------
#
# Copyright (C) 2002 Ryan C. Gordon (icculus@icculus.org)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#-----------------------------------------------------------------------------
21
22
use strict;
23
use warnings;
24
use File::Copy;
25
26
27
28
29
30
31
#-----------------------------------------------------------------------------#
# CONFIGURATION VARIABLES: Change to suit your needs... #
#-----------------------------------------------------------------------------#
# Where the home dirs reside. $homebasedir/username/.plan is where we look...
# Note that THIS MUST NOT HAVE THE TRAILING DIR SEPARATOR!
32
my $homebasedir = '/home';
33
34
35
# This is the directory to move planfiles to. Note that THIS MUST NOT HAVE
# THE TRAILING DIR SEPARATOR!
36
37
my $fingerspace = '/fingerspace';
38
39
40
41
42
43
#-----------------------------------------------------------------------------#
# The rest is probably okay without you laying yer dirty mits on it. #
#-----------------------------------------------------------------------------#
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
sub chown_by_name {
my($user, $file) = @_;
chown((getpwnam($user))[2], (getpwnam('root'))[3], $file) == 1 or
die("chown on $file failed: $!\n");
}
sub check_dir {
my $user = shift;
my $nofingerpath = "$homebasedir/$user/.nofinger";
my $homepath = "$homebasedir/$user/.plan";
my $fingerpath = "$fingerspace/$user";
if (($user eq '.') or ($user eq '..')) {
return;
}
if (not -d "$homebasedir/$user") {
62
return if ($user eq '.keep'); # gentoo fix.
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
print(" - $homebasedir/$user isn't a directory. Wierd.\n");
return;
}
# ...since the user can WRITE to an existing file, but not create one
# in the fingerspace, we need to create it if it doesn't exist...
if (not -e $fingerpath) {
print(" + Creating $fingerpath ...\n");
# God, I'm lazy. :)
`touch $fingerpath`;
if (not -e $fingerpath) {
print("Failed to create.\n");
return;
}
}
# always fix permissions ...
chmod(0644, $fingerpath) or die("Failed to chmod $fingerpath: $!\n");
chown_by_name($user, $fingerpath);
if (not -e $homepath) {
print(" + symlinking missing file $homepath to $fingerpath ...\n");
symlink($fingerpath, $homepath) or die("symlink failed: $!\n");
return;
}
# (For now, we'll assume that a symlink means we've moved it already.)
if (-l $homepath) {
if (readlink($homepath) ne $fingerpath) {
print(" - There's an UNEXPECTED SYMLINK at $homepath ...\n");
}
return;
}
if (-d $homepath) {
print(" - Uh...there's a DIRECTORY at $homepath ...\n");
return;
}
if (-d $fingerpath) {
print(" - Uh...there's a DIRECTORY at $fingerpath ...\n");
return;
}
if (-e $nofingerpath) {
print(" - $nofingerpath exists. Explicitly skipping ...\n");
return;
}
print(" + Moving $homepath to $fingerpath ... \n");
113
move($homepath, $fingerpath) or die("move failed: $!\n");
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
symlink($fingerpath, $homepath) or die("symlink failed: $!\n");
}
# the mainline ...
opendir(DIRH, $homebasedir) or die("Can't open $homebasedir: $!\n");
my @homedirs = readdir(DIRH);
closedir(DIRH);
foreach (@homedirs) {
check_dir($_);
}
# end of IcculusFinger_planmove.pl ...