/[Intel-AMT]/trunk/lib/Intel/AMT/SOAP.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/lib/Intel/AMT/SOAP.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.26