cvsrepo_ctrl.proxy.in 6.36 KB
Newer Older
1
2
3
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2005, 2006 University of Utah and the Flux Group.
5
6
7
8
9
10
11
12
13
14
15
16
# All rights reserved.
#
use English;
use Errno;
use Fcntl ':flock';
use Getopt::Std;

#
# Install new cvsd.conf file ...
#
sub usage()
{
17
    print(STDOUT "Usage: cvsrepo_ctrl.proxy [-m]\n");
18
19
    exit(-1);
}
20
21
my $optlist   = "m";
my $domounts  = 0;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";

my $CVSDCONF	= "/usr/local/etc/cvsd/cvsd.conf";
my $CVSDCONFNEW	= "/usr/local/etc/cvsd/cvsd.conf.new";
my $CVSDCONFOLD	= "/usr/local/etc/cvsd/cvsd.conf.backup";
my $HEADFILE    = "$TB/lib/cvsd.conf.head";
my $STARTPROG   = "/usr/local/etc/rc.d/cvsd.sh";
my $REPODIR     = "cvsrepos";
my $MOUNT	= "/sbin/mount -o ro";
my $UNMOUNT     = "/sbin/umount";
my %pubrepos    = ();

#
# We don't want to run this script unless its the real version.
#
if ($UID != 0) {
43
44
    die("*** $0:\n".
	"    Must be root to run this script!");
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
}

# un-taint path
$ENV{'PATH'} = '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

# Turn off line buffering on output
$| = 1;

# Protos
sub fatal($);

#
# Testbed Support libraries
# 
use lib "@prefix@/lib";
use libtestbed;

63
64
65
my $PROJREPODIR = PROJDIR() . "/$REPODIR";
my $JAILREPODIR = "/var/cvsjail/$REPODIR";

66
67
68
69
70
71
72
73
74
75
76
77
78
79
#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{m})) {
    $domounts = 1;
}
usage()
    if (@ARGV);

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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#
# Take our input and get the list of repos we need to make public.
#
while (<STDIN>) {
    if ($_ =~ /^([^\s]*)$/) {
	$pubrepos{$1} = $1;
    }
}

#
# Generate a warning so that no one tries to edit the file by hand
#
open(CONF, ">$CVSDCONFNEW") ||
    fatal("Could not open $CVSDCONFNEW!");

print CONF
    "#\n".
    "# ******************************************************************\n".
    "# DO NOT EDIT THIS FILE. IT IS A CREATION, A FIGMENT, A CONTRIVANCE!\n".
    "# ******************************************************************\n".
    "#\n";
close(CONF);
chmod(0644, $CVSDCONFNEW);

#
# Now tack on the head part of the file.
#
system("cat $HEADFILE >> $CVSDCONFNEW") == 0 or
    fatal("Failed to concat $HEADFILE to $CVSDCONFNEW\n");

#
# Now the tail of the file.
#
open(CONF, ">>$CVSDCONFNEW") ||
    fatal("Could not open $CVSDCONFNEW for appending!");

print CONF "\n";
print CONF "#\n";
print CONF "# DO NOT EDIT below this point. Auto generated entries!\n";
print CONF "#\n";

foreach my $repo (keys(%pubrepos)) {
    print CONF "Repos /$REPODIR/$repo\n";
}
close(CONF);

#
# Back up the existing config, and then mv in the new one.
#
system("cp $CVSDCONF $CVSDCONFOLD") == 0 or
    fatal("Could not back up $CVSDCONF to $CVSDCONFOLD");

system("mv $CVSDCONFNEW $CVSDCONF") == 0 or
    fatal("Could not mv $CVSDCONFNEW to $CVSDCONF");

# Avoid accidental editing.
chmod(0444, $CVSDCONF);

138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#
# In mounts mode, just do the NFS mounts and exit. This is intended to
# be called at bootup, from boss.
#
if ($domounts) {
    foreach my $repo (keys(%pubrepos)) {
	my $dir = $repo;

	if (! -d "$JAILREPODIR/$dir") {
	    system("/bin/mkdir $JAILREPODIR/$dir") == 0 or
		fatal("Could not mkdir $JAILREPODIR/$dir");
	}

	#
	# Now mount it, if not already done so.
	#
	if (! -d "$JAILREPODIR/$dir/CVSROOT") {
	    print "Mount: localhost:${PROJREPODIR}/$dir $JAILREPODIR/$dir\n";
	    
	    system("$MOUNT localhost:${PROJREPODIR}/$dir $JAILREPODIR/$dir");
	    if ($?) {
		fatal("Could not NFS mount localhost:${PROJREPODIR}/$dir");
	    }
	}
    }
    exit(0);
}

166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#
# Must stop cvsd before doing the next step.
# 
system("$STARTPROG stop") == 0 or
    fatal("Could not stop cvsd!");

#
# Okay, now do the mounts and unmounts into the jail tree. Each pub
# repo gets a mount and each repo that is no longer pub, gets an unmount.
#
# First see whats already there. The only record I keep is the list of
# subdirs that exist already.
#
opendir(DIR, $JAILREPODIR) or
    fatal("Could not opdendir $JAILREPODIR: $!");
my @dirfiles = readdir(DIR);
closedir(DIR);

#
# Get rid of ones that are no longer supposed to be public.
# 
foreach my $dir (@dirfiles) {
    next
	if ($dir eq "." || $dir eq "..");

    #
    # Any directory that is not in the list we got from boss, needs
    # an unmount and rmdir; it is no longer public.
    # 
    if (! exists($pubrepos{$dir})) {
	system("$UNMOUNT $JAILREPODIR/$dir") == 0 or
	    fatal("Could not unmount $JAILREPODIR/$dir");
	system("/bin/rmdir $JAILREPODIR/$dir") == 0 or
	    fatal("Could not rmdir $JAILREPODIR/$dir");
    }
    else {
	#
	# Already there, so delete from the list of ones we need to add.
	#
	delete($pubrepos{$dir});
    }
}

#
# Now add any new ones.
#
foreach my $repo (keys(%pubrepos)) {
    my $dir = $repo;
	
    system("/bin/mkdir $JAILREPODIR/$dir") == 0 or
	fatal("Could not mkdir $JAILREPODIR/$dir");

    #
    # Now mount it; if it fails then remove the directory too so as
    # not to mess up the next time it runs. 
    #
    system("$MOUNT localhost:${PROJREPODIR}/$dir $JAILREPODIR/$dir");
    if ($?) {
	system("/bin/rm -r $JAILREPODIR/$dir");
	fatal("Could not NFS mount localhost:${PROJREPODIR}/$dir");
    }

    #
    # Setup an anoncvs password entry for the directory.
    #
    system("/bin/echo 'anoncvs::nobody' > ".
	   "$PROJREPODIR/$dir/CVSROOT/passwd") == 0 or
	fatal("Could not create $PROJREPODIR/$dir/CVSROOT/passwd!");

    system("/bin/echo 'anonymous::nobody' >> ".
	   "$PROJREPODIR/$dir/CVSROOT/passwd") == 0 or
	fatal("Could not create $PROJREPODIR/$dir/CVSROOT/passwd!");

    #
    # And now generate a config file that makes pserver happy. Just
    # blow away the existing one; it should not matter.
    # 
    system("/bin/rm -f $PROJREPODIR/$dir/CVSROOT/config ".
	   "           $PROJREPODIR/$dir/CVSROOT/config,v") == 0 or
	fatal("Could not rm $PROJREPODIR/$dir/CVSROOT/config!");

    open(CONF, "> $PROJREPODIR/$dir/CVSROOT/config") or
	fatal("Could not open $PROJREPODIR/$dir/CVSROOT/config!");
    print CONF "SystemAuth=no\n";
    print CONF "LockDir=/var/cvslock\n";
    close(CONF);
}

#
# Okay, restart the cvsd process. Too bad there is no HUP handler to reload
# the config file, as this is going to kill anyone currently connected. 
#
system("$STARTPROG start") == 0 or
    fatal("Could not restart cvsd!");

exit(0);

sub fatal($) {
    my ($msg) = @_;

    die("*** $0:\n".
	"    $msg\n");
}