1 |
package Intel::AMT::SOAP; |
2 |
|
3 |
# based on amttool from amtterm 1.2 from http://dl.bytesex.org/releases/amtterm/ |
4 |
|
5 |
use strict; |
6 |
use warnings; |
7 |
use SOAP::Lite; |
8 |
#use SOAP::Lite +trace => 'all'; |
9 |
use Data::Dump qw/dump/; |
10 |
|
11 |
use lib 'lib'; |
12 |
|
13 |
my $amt_host = $ENV{'AMT_HOST'}; |
14 |
my $amt_port = "16992"; |
15 |
$main::amt_user = "admin"; |
16 |
$main::amt_pass = $ENV{'AMT_PASSWORD'}; |
17 |
my $amt_debug = 0; |
18 |
$amt_debug = $ENV{'AMT_DEBUG'} if defined($ENV{'AMT_DEBUG'}); |
19 |
|
20 |
my $amt_version; |
21 |
|
22 |
############################################################################# |
23 |
# data |
24 |
|
25 |
my @ps = ("S0", "S1", "S2", "S3", "S4", "S5 (soft-off)", "S4/S5", "Off"); |
26 |
|
27 |
# incomplete list |
28 |
my %pt_status = ( |
29 |
0x0 => "success", |
30 |
0x1 => "internal error", |
31 |
0x3 => "invalid pt_mode", |
32 |
0xc => "invalid name", |
33 |
0xf => "invalid byte_count", |
34 |
0x10 => "not permitted", |
35 |
0x17 => "max limit_reached", |
36 |
0x18 => "invalid auth_type", |
37 |
0x1a => "invalid dhcp_mode", |
38 |
0x1b => "invalid ip_address", |
39 |
0x1c => "invalid domain_name", |
40 |
0x20 => "invalid provisioning_state", |
41 |
0x22 => "invalid time", |
42 |
0x23 => "invalid index", |
43 |
0x24 => "invalid parameter", |
44 |
0x25 => "invalid netmask", |
45 |
0x26 => "flash write_limit_exceeded", |
46 |
0x800 => "network if_error_base", |
47 |
0x801 => "unsupported oem_number", |
48 |
0x802 => "unsupported boot_option", |
49 |
0x803 => "invalid command", |
50 |
0x804 => "invalid special_command", |
51 |
0x805 => "invalid handle", |
52 |
0x806 => "invalid password", |
53 |
0x807 => "invalid realm", |
54 |
0x808 => "storage acl_entry_in_use", |
55 |
0x809 => "data missing", |
56 |
0x80a => "duplicate", |
57 |
0x80b => "eventlog frozen", |
58 |
0x80c => "pki missing_keys", |
59 |
0x80d => "pki generating_keys", |
60 |
0x80e => "invalid key", |
61 |
0x80f => "invalid cert", |
62 |
0x810 => "cert key_not_match", |
63 |
0x811 => "max kerb_domain_reached", |
64 |
0x812 => "unsupported", |
65 |
0x813 => "invalid priority", |
66 |
0x814 => "not found", |
67 |
0x815 => "invalid credentials", |
68 |
0x816 => "invalid passphrase", |
69 |
0x818 => "no association", |
70 |
); |
71 |
|
72 |
|
73 |
############################################################################# |
74 |
# soap setup |
75 |
|
76 |
my ($nas, $sas, $rcs); |
77 |
|
78 |
sub SOAP::Transport::HTTP::Client::get_basic_credentials { |
79 |
return $main::amt_user => $main::amt_pass; |
80 |
} |
81 |
|
82 |
sub init() { |
83 |
my $proxybase = "http://$amt_host:$amt_port"; |
84 |
my $schemabase = "http://schemas.intel.com/platform/client"; |
85 |
|
86 |
$nas = SOAP::Lite->new( |
87 |
proxy => "$proxybase/NetworkAdministrationService", |
88 |
default_ns => "$schemabase/NetworkAdministration/2004/01"); |
89 |
$sas = SOAP::Lite->new( |
90 |
proxy => "$proxybase/SecurityAdministrationService", |
91 |
default_ns => "$schemabase/SecurityAdministration/2004/01"); |
92 |
$rcs = SOAP::Lite->new( |
93 |
proxy => "$proxybase/RemoteControlService", |
94 |
default_ns => "$schemabase/RemoteControl/2004/01"); |
95 |
|
96 |
$nas->autotype(0); |
97 |
$sas->autotype(0); |
98 |
$rcs->autotype(0); |
99 |
|
100 |
warn $proxybase; |
101 |
|
102 |
$amt_version = $sas->GetCoreVersion()->paramsout; |
103 |
} |
104 |
|
105 |
sub _soap { |
106 |
my $name = shift; |
107 |
|
108 |
my $proxybase = "http://$amt_host:$amt_port"; |
109 |
my $schemabase = "http://schemas.intel.com/platform/client"; |
110 |
|
111 |
warn "call_soap $proxybase $name ",dump( @_ ); |
112 |
|
113 |
my $soap = SOAP::Lite->new( |
114 |
proxy => "$proxybase/${name}Service", |
115 |
default_ns => "$schemabase/$name/2004/01"); |
116 |
|
117 |
$soap->autotype(0); |
118 |
|
119 |
if ( @_ ) { |
120 |
do_soap($soap, "RemoteControl", @_) |
121 |
} else { |
122 |
return $soap; |
123 |
} |
124 |
} |
125 |
|
126 |
|
127 |
############################################################################# |
128 |
# functions |
129 |
|
130 |
sub usage() { |
131 |
print STDERR <<EOF; |
132 |
|
133 |
This utility can talk to Intel AMT managed machines. |
134 |
|
135 |
usage: amttool <hostname> [ <command> ] [ <arg(s)> ] |
136 |
commands: |
137 |
info - print some machine info (default). |
138 |
reset - reset machine. |
139 |
powerup - turn on machine. |
140 |
powerdown - turn off machine. |
141 |
powercycle - powercycle machine. |
142 |
|
143 |
AMT 2.5+ only: |
144 |
netinfo - print network config. |
145 |
netconf <args> - configure network (check manpage). |
146 |
|
147 |
Password is passed via AMT_PASSWORD environment variable. |
148 |
|
149 |
EOF |
150 |
} |
151 |
|
152 |
sub print_result($) { |
153 |
my $ret = shift; |
154 |
my $rc = $ret->result; |
155 |
my $msg; |
156 |
|
157 |
if (!defined($rc)) { |
158 |
$msg = "soap failure"; |
159 |
warn dump( $ret->faultdetail ); |
160 |
} elsif (!defined($pt_status{$rc})) { |
161 |
$msg = sprintf("unknown pt_status code: 0x%x", $rc); |
162 |
} else { |
163 |
$msg = "pt_status: " . $pt_status{$rc}; |
164 |
} |
165 |
printf "result: %s\n", $msg; |
166 |
} |
167 |
|
168 |
sub print_paramsout($) { |
169 |
my $ret = shift; |
170 |
my @paramsout = $ret->paramsout; |
171 |
print "params: " . join(", ", @paramsout) . "\n"; |
172 |
} |
173 |
|
174 |
sub print_hash { |
175 |
my $hash = shift; |
176 |
my $in = shift; |
177 |
my $wi = shift; |
178 |
|
179 |
foreach my $item (sort keys %{$hash}) { |
180 |
if (ref($hash->{$item}) eq "HASH") { |
181 |
# printf "%*s%s\n", $in, "", $item; |
182 |
next; |
183 |
} |
184 |
printf "%*s%-*s%s\n", $in, "", $wi, $item, $hash->{$item}; |
185 |
} |
186 |
} |
187 |
|
188 |
sub print_hash_ipv4 { |
189 |
my $hash = shift; |
190 |
my $in = shift; |
191 |
my $wi = shift; |
192 |
|
193 |
foreach my $item (sort keys %{$hash}) { |
194 |
my $addr = sprintf("%d.%d.%d.%d", |
195 |
$hash->{$item} / 256 / 256 / 256, |
196 |
$hash->{$item} / 256 / 256 % 256, |
197 |
$hash->{$item} / 256 % 256, |
198 |
$hash->{$item} % 256); |
199 |
printf "%*s%-*s%s\n", $in, "", $wi, $item, $addr; |
200 |
} |
201 |
} |
202 |
|
203 |
sub do_soap { |
204 |
my $soap = shift; |
205 |
my $name = shift; |
206 |
my @args = @_; |
207 |
my $method; |
208 |
|
209 |
$method = SOAP::Data->name($name) |
210 |
->attr( { xmlns => $soap->ns } ); |
211 |
|
212 |
if ($amt_debug) { |
213 |
print "-- \n"; |
214 |
open XML, "| xmllint --format -"; |
215 |
print XML $soap->serializer->envelope(method => $method, @_); |
216 |
close XML; |
217 |
print "-- \n"; |
218 |
} |
219 |
|
220 |
my $ret = $soap->call($method, @args); |
221 |
print_result($ret); |
222 |
return $ret; |
223 |
} |
224 |
|
225 |
sub check_amt_version { |
226 |
my $major = shift; |
227 |
my $minor = shift; |
228 |
|
229 |
$amt_version =~ m/^(\d+).(\d+)/; |
230 |
return if $1 > $major; |
231 |
return if $1 == $major && $2 >= $minor; |
232 |
die "version mismatch (need >= $major.$minor, have $amt_version)"; |
233 |
} |
234 |
|
235 |
sub print_general_info() { |
236 |
printf "### AMT info on machine '%s' ###\n", $amt_host; |
237 |
|
238 |
printf "AMT version: %s\n", $amt_version; |
239 |
|
240 |
my $hostname = $nas->GetHostName()->paramsout; |
241 |
my $domainname = $nas->GetDomainName()->paramsout; |
242 |
printf "Hostname: %s.%s\n", $hostname, $domainname; |
243 |
|
244 |
my $powerstate = $rcs->GetSystemPowerState()->paramsout; |
245 |
printf "Powerstate: %s\n", $ps [ $powerstate & 0x0f ]; |
246 |
} |
247 |
|
248 |
sub print_network_info() { |
249 |
my $ret; |
250 |
|
251 |
$ret = $nas->EnumerateInterfaces(); |
252 |
my @if = $ret->paramsout; |
253 |
foreach my $if (@if) { |
254 |
printf "Network Interface %s:\n", $if; |
255 |
my $arg = SOAP::Data->name('InterfaceHandle' => $if); |
256 |
$ret = $nas->GetInterfaceSettings($arg); |
257 |
my $desc = $ret->paramsout; |
258 |
print_hash($ret->paramsout, 4, 32); |
259 |
print_hash_ipv4($ret->paramsout->{'IPv4Parameters'}, 8, 28); |
260 |
} |
261 |
} |
262 |
|
263 |
sub ipv4_addr($$) { |
264 |
my $name = shift; |
265 |
my $ipv4 = shift; |
266 |
|
267 |
$ipv4 =~ m/(\d+).(\d+).(\d+).(\d+)/ or die "parse ipv4 address: $ipv4"; |
268 |
my $num = $1 * 256 * 256 * 256 + |
269 |
$2 * 256 * 246 + |
270 |
$3 * 256 + |
271 |
$4; |
272 |
printf STDERR "ipv4 %-24s: %-16s -> %d\n", $name, $ipv4, $num |
273 |
if $amt_debug; |
274 |
return SOAP::Data->name($name => $num); |
275 |
} |
276 |
|
277 |
sub configure_network { |
278 |
my $if = shift; |
279 |
my $link = shift; |
280 |
my $ip = shift; |
281 |
my $mask = shift; |
282 |
my $gw = shift; |
283 |
my $dns1 = shift; |
284 |
my $dns2 = shift; |
285 |
|
286 |
my $mode; |
287 |
my @ifdesc; |
288 |
my @ipv4; |
289 |
|
290 |
my $method; |
291 |
my @args; |
292 |
|
293 |
# build argument structs ... |
294 |
die "no interface" if !defined($if); |
295 |
die "no linkpolicy" if !defined($link); |
296 |
if (defined($ip)) { |
297 |
$mode = "SEPARATE_MAC_ADDRESS"; |
298 |
die "no ip mask" if !defined($mask); |
299 |
die "no default gw" if !defined($gw); |
300 |
$dns1 = $gw if !defined($dns1); |
301 |
$dns2 = "0.0.0.0" if !defined($dns2); |
302 |
push (@ipv4, ipv4_addr("LocalAddress", $ip)); |
303 |
push (@ipv4, ipv4_addr("SubnetMask", $mask)); |
304 |
push (@ipv4, ipv4_addr("DefaultGatewayAddress", $gw)); |
305 |
push (@ipv4, ipv4_addr("PrimaryDnsAddress", $dns1)); |
306 |
push (@ipv4, ipv4_addr("SecondaryDnsAddress", $dns2)); |
307 |
} else { |
308 |
$mode = "SHARED_MAC_ADDRESS"; |
309 |
# no ip info -- use DHCP |
310 |
} |
311 |
|
312 |
push (@ifdesc, SOAP::Data->name("InterfaceMode" => $mode)); |
313 |
push (@ifdesc, SOAP::Data->name("LinkPolicy" => $link)); |
314 |
push (@ifdesc, SOAP::Data->name("IPv4Parameters" => |
315 |
\SOAP::Data->value(@ipv4))) |
316 |
if @ipv4; |
317 |
|
318 |
push (@args, SOAP::Data->name("InterfaceHandle" => $if)); |
319 |
push (@args, SOAP::Data->name("InterfaceDescriptor" => |
320 |
\SOAP::Data->value(@ifdesc))); |
321 |
|
322 |
# perform call |
323 |
do_soap($nas, "SetInterfaceSettings", @args); |
324 |
} |
325 |
|
326 |
|
327 |
sub command { |
328 |
my ($amt_command,$amt_arg) = @_; |
329 |
|
330 |
init; |
331 |
|
332 |
if ($amt_command eq "info") { |
333 |
print_general_info; |
334 |
} elsif ($amt_command eq "netinfo") { |
335 |
check_amt_version(2,5); |
336 |
print_network_info; |
337 |
} elsif ($amt_command eq "netconf") { |
338 |
check_amt_version(2,5); |
339 |
configure_network(@_); |
340 |
} elsif ($amt_command =~ m/^(reset|powerup|powerdown|powercycle)$/) { |
341 |
remote_control($amt_command, $amt_arg); |
342 |
} else { |
343 |
print "unknown command: $amt_command\n"; |
344 |
} |
345 |
|
346 |
} |
347 |
|
348 |
warn 'loaded'; |
349 |
|
350 |
1; |