rc.localize 3.19 KB
Newer Older
1
2
3
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2004, 2005 University of Utah and the Flux Group.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# All rights reserved.
#
use English;
use Getopt::Std;

sub usage()
{
    print "Usage: " .
	scriptname() . " [-j vnodeid] boot|shutdown|reconfig|reset\n";
    exit(1);
}
my $optlist = "j:";
my $action  = "boot";

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

# Drag in path stuff so we can find emulab stuff.
BEGIN { require "/etc/emulab/paths.pm"; import emulabpaths; }

# Only root.
if ($EUID != 0) {
    die("*** $0:\n".
	"    Must be root to run this script!\n");
}

#
# Load the OS independent support library. It will load the OS dependent
# library and initialize itself. 
# 
use libsetup;
use libtmcc;
use librc;

#
# Not all clients support this.
#
exit(0)
    if (REMOTE() || JAILED() || CONTROL());

# Protos.
sub doboot();
sub doshutdown();
sub doreconfig();
sub docleanup();

# Parse command line.
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{'j'})) {
    my $vnodeid = $options{'j'};
    libsetup_setvnodeid($vnodeid);
}
# Allow default above.
if (@ARGV) {
    $action = $ARGV[0];
}

# Execute the action.
SWITCH: for ($action) {
    /^boot$/i && do {
	doboot();
	last SWITCH;
    };
    /^shutdown$/i && do {
	doshutdown();
	last SWITCH;
    };
    /^reconfig$/i && do {
	doreconfig();
	last SWITCH;
    };
    /^reset$/i && do {
	docleanup();
	last SWITCH;
    };
    fatal("Invalid action: $action\n");
}
exit(0);

# More protos
sub donodeid();
sub docreator();

#
# Boot Action.
#
sub doboot()
{
    my @tmccresults;
    my @pubkeys     = ();

    print STDOUT "Checking Testbed localization configuration ... \n";

    if (tmcc(TMCCCMD_LOCALIZATION, undef, \@tmccresults) < 0) {
	fatal("Could not get localization info from server!");
    }
    # Important; if no results then do nothing. 
    return 0
	if (! @tmccresults);

    #
    # Go through and see what we got. For now, we expect just ssh keys
    # 
    foreach my $str (@tmccresults) {
	if ($str =~ /^ROOTPUBKEY='(.*)'$/) {
	    push(@pubkeys, $1);
	}
	else {
	    warning("Bad localization line: $str");
	}
    }

    #
    # Write new pubkeys to root authkeys file. As a safety mechanism, back
    # up old authkeys to authkeys2 file, which is also used by openssh sshd. 
    #
    if (@pubkeys) {
	my $authkeys  = "/root/.ssh/authorized_keys";
	my $authkeys2 = "/root/.ssh/authorized_keys2";
	
127
128
129
130
	if (-e "$authkeys") {
	    system("cp -pf $authkeys $authkeys2") == 0
		or fatal("Could not backup root ssh authorized_keys file");
	}
131
132
133
134
135
136
137
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
166
167
168
169
170
171
172
173
174
175

	if (!open(AUTHKEYS, "> ${authkeys}.new")) {
	    warning("Could not open ${authkeys}.new: $!");
	    return -1;
	}
	print AUTHKEYS "#\n";
	print AUTHKEYS "# DO NOT EDIT! This file auto generated at bootup.\n";
	print AUTHKEYS "#\n";
    
	foreach my $key (@pubkeys) {
	    print AUTHKEYS "$key\n";
	}
	close(AUTHKEYS);

	system("mv -f ${authkeys}.new ${authkeys}") == 0 
	    or warning("Could not mv ${authkeys}.new to ${authkeys}");
	
	return 0;
    }
}

#
# Shutdown Action.
#
sub doshutdown()
{
    # Nothing to do
}

#
# Node Reconfig Action (without rebooting).
#
sub doreconfig()
{
    doshutdown();
    return doboot();
}

#
# Node cleanup action (node is reset to completely clean state).
#
sub docleanup()
{
}