Code

Ignore test TASKBEGIN messages
[gosa.git] / trunk / gosa-si / server / events / opsi_com.pm
1 ## @file
2 # @details A GOsa-SI-server event module containing all functions for message handling.
3 # @brief Implementation of an event module for GOsa-SI-server. 
6 package opsi_com;
7 use Exporter;
8 @ISA = qw(Exporter);
9 my @events = (
10     "get_events",
11     "opsi_install_client",
12     "opsi_get_netboot_products",  
13     "opsi_get_local_products",
14     "opsi_get_client_hardware",
15     "opsi_get_client_software",
16     "opsi_get_product_properties",
17     "opsi_set_product_properties",
18     "opsi_list_clients",
19     "opsi_del_client",
20     "opsi_add_client",
21     "opsi_modify_client",
22     "opsi_add_product_to_client",
23     "opsi_del_product_from_client",
24    );
25 @EXPORT = @events;
27 use strict;
28 use warnings;
29 use GOSA::GosaSupportDaemon;
30 use Data::Dumper;
31 use XML::Quote qw(:all);
34 BEGIN {}
36 END {}
38 ## @method get_events()
39 # A brief function returning a list of functions which are exported by importing the module.
40 # @return List of all provided functions
41 sub get_events {
42     return \@events;
43 }
45 ## @method opsi_add_product_to_client
46 # Adds an Opsi product to an Opsi client.
47 # @param msg - STRING - xml message with tags hostId and productId
48 # @param msg_hash - HASHREF - message information parsed into a hash
49 # @param session_id - INTEGER - POE session id of the processing of this message
50 # @return out_msg - STRING - feedback to GOsa in success and error case
51 sub opsi_add_product_to_client {
52     my ($msg, $msg_hash, $session_id) = @_;
53     my $header = @{$msg_hash->{'header'}}[0];
54     my $source = @{$msg_hash->{'source'}}[0];
55     my $target = @{$msg_hash->{'target'}}[0];
56     my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
57     my ($hostId, $productId);
58     my $error = 0;
60     # Build return message
61     my $out_hash = &main::create_xml_hash("answer_$header", $main::server_address, $source);
62     if (defined $forward_to_gosa) {
63         &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
64     }
66     # Sanity check of needed parameter
67     if ((not exists $msg_hash->{'hostId'}) || (@{$msg_hash->{'hostId'}} != 1) || (@{$msg_hash->{'hostId'}}[0] eq ref 'HASH'))  {
68         $error++;
69         &add_content2xml_hash($out_hash, "error_string", "no hostId specified or hostId tag invalid");
70         &add_content2xml_hash($out_hash, "error", "hostId");
71         &main::daemon_log("$session_id ERROR: no hostId specified or hostId tag invalid: $msg", 1); 
73     }
74     if ((not exists $msg_hash->{'productId'}) || (@{$msg_hash->{'productId'}} != 1) || (@{$msg_hash->{'productId'}}[0] eq ref 'HASH')) {
75         $error++;
76         &add_content2xml_hash($out_hash, "error_string", "no productId specified or productId tag invalid");
77         &add_content2xml_hash($out_hash, "error", "productId");
78         &main::daemon_log("$session_id ERROR: no productId specified or procutId tag invalid: $msg", 1); 
79     }
81     if (not $error) {
82         # Get hostId
83         $hostId = @{$msg_hash->{'hostId'}}[0];
84         &add_content2xml_hash($out_hash, "hostId", $hostId);
86         # Get productID
87         $productId = @{$msg_hash->{'productId'}}[0];
88         &add_content2xml_hash($out_hash, "productId", $productId);
90         # Do an action request for all these -> "setup".
91         my $callobj = {
92             method  => 'setProductActionRequest',
93             params  => [ $productId, $hostId, "setup" ],
94             id  => 1, }; 
96         my $sres = $main::opsi_client->call($main::opsi_url, $callobj);
97         my ($sres_err, $sres_err_string) = &check_opsi_res($sres);
98         if ($sres_err){
99             &main::daemon_log("$session_id ERROR: cannot add product: ".$sres_err_string, 1);
100             &add_content2xml_hash($out_hash, "error", $sres_err_string);
101         }
102     } 
104     # return message
105     return ( &create_xml_string($out_hash) );
108 ## @method opsi_del_product_from_client
109 # Deletes an Opsi-product from an Opsi-client. 
110 # @param msg - STRING - xml message with tags hostId and productId
111 # @param msg_hash - HASHREF - message information parsed into a hash
112 # @param session_id - INTEGER - POE session id of the processing of this message
113 # @return out_msg - STRING - feedback to GOsa in success and error case
114 sub opsi_del_product_from_client {
115     my ($msg, $msg_hash, $session_id) = @_;
116     my $header = @{$msg_hash->{'header'}}[0];
117     my $source = @{$msg_hash->{'source'}}[0];
118     my $target = @{$msg_hash->{'target'}}[0];
119     my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
120     my ($hostId, $productId);
121     my $error = 0;
122     my ($sres, $sres_err, $sres_err_string);
124     # Build return message
125     my $out_hash = &main::create_xml_hash("answer_$header", $main::server_address, $source);
126     if (defined $forward_to_gosa) {
127         &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
128     }
130     # Sanity check of needed parameter
131     if ((not exists $msg_hash->{'hostId'}) || (@{$msg_hash->{'hostId'}} != 1) || (@{$msg_hash->{'hostId'}}[0] eq ref 'HASH'))  {
132         $error++;
133         &add_content2xml_hash($out_hash, "error_string", "no hostId specified or hostId tag invalid");
134         &add_content2xml_hash($out_hash, "error", "hostId");
135         &main::daemon_log("$session_id ERROR: no hostId specified or hostId tag invalid: $msg", 1); 
137     }
138     if ((not exists $msg_hash->{'productId'}) || (@{$msg_hash->{'productId'}} != 1) || (@{$msg_hash->{'productId'}}[0] eq ref 'HASH')) {
139         $error++;
140         &add_content2xml_hash($out_hash, "error_string", "no productId specified or productId tag invalid");
141         &add_content2xml_hash($out_hash, "error", "productId");
142         &main::daemon_log("$session_id ERROR: no productId specified or procutId tag invalid: $msg", 1); 
143     }
145     # All parameter available
146     if (not $error) {
147         # Get hostId
148         $hostId = @{$msg_hash->{'hostId'}}[0];
149         &add_content2xml_hash($out_hash, "hostId", $hostId);
151         # Get productID
152         $productId = @{$msg_hash->{'productId'}}[0];
153         &add_content2xml_hash($out_hash, "productId", $productId);
156 #TODO : check the results for more than one entry which is currently installed
157         #$callobj = {
158         #    method  => 'getProductDependencies_listOfHashes',
159         #    params  => [ $productId ],
160         #    id  => 1, };
161         #
162         #my $sres = $main::opsi_client->call($main::opsi_url, $callobj);
163         #my ($sres_err, $sres_err_string) = &check_opsi_res($sres);
164         #if ($sres_err){
165         #  &main::daemon_log("ERROR: cannot perform dependency check: ".$sres_err_string, 1);
166         #  &add_content2xml_hash($out_hash, "error", $sres_err_string);
167         #  return ( &create_xml_string($out_hash) );
168         #}
171         # Check to get product action list 
172         my $callobj = {
173             method  => 'getPossibleProductActions_list',
174             params  => [ $productId ],
175             id  => 1, };
176         $sres = $main::opsi_client->call($main::opsi_url, $callobj);
177         ($sres_err, $sres_err_string) = &check_opsi_res($sres);
178         if ($sres_err){
179             &main::daemon_log("$session_id ERROR: cannot get product action list: ".$sres_err_string, 1);
180             &add_content2xml_hash($out_hash, "error", $sres_err_string);
181             $error++;
182         }
183     }
185     # Check action uninstall of product
186     if (not $error) {
187         my $uninst_possible= 0;
188         foreach my $r (@{$sres->result}) {
189             if ($r eq 'uninstall') {
190                 $uninst_possible= 1;
191             }
192         }
193         if (!$uninst_possible){
194             &main::daemon_log("$session_id ERROR: cannot uninstall product '$productId', product do not has the action 'uninstall'", 1);
195             &add_content2xml_hash($out_hash, "error", "cannot uninstall product '$productId', product do not has the action 'uninstall'");
196             $error++;
197         }
198     }
200     # Set product state to "none"
201     # Do an action request for all these -> "setup".
202     if (not $error) {
203         my $callobj = {
204             method  => 'setProductActionRequest',
205             params  => [ $productId, $hostId, "none" ],
206             id  => 1, 
207         }; 
208         $sres = $main::opsi_client->call($main::opsi_url, $callobj);
209         ($sres_err, $sres_err_string) = &check_opsi_res($sres);
210         if ($sres_err){
211             &main::daemon_log("$session_id ERROR: cannot delete product: ".$sres_err_string, 1);
212             &add_content2xml_hash($out_hash, "error", $sres_err_string);
213         }
214     }
216     # Return message
217     return ( &create_xml_string($out_hash) );
220 ## @method opsi_add_client
221 # Adds an Opsi client to Opsi.
222 # @param msg - STRING - xml message with tags hostId and macaddress
223 # @param msg_hash - HASHREF - message information parsed into a hash
224 # @param session_id - INTEGER - POE session id of the processing of this message
225 # @return out_msg - STRING - feedback to GOsa in success and error case
226 sub opsi_add_client {
227     my ($msg, $msg_hash, $session_id) = @_;
228     my $header = @{$msg_hash->{'header'}}[0];
229     my $source = @{$msg_hash->{'source'}}[0];
230     my $target = @{$msg_hash->{'target'}}[0];
231     my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
232     my ($hostId, $mac);
233     my $error = 0;
234     my ($sres, $sres_err, $sres_err_string);
236     # Build return message with twisted target and source
237     my $out_hash = &main::create_xml_hash("answer_$header", $main::server_address, $source);
238     if (defined $forward_to_gosa) {
239         &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
240     }
242     # Sanity check of needed parameter
243     if ((not exists $msg_hash->{'hostId'}) || (@{$msg_hash->{'hostId'}} != 1) || (@{$msg_hash->{'hostId'}}[0] eq ref 'HASH'))  {
244         $error++;
245         &add_content2xml_hash($out_hash, "error_string", "no hostId specified or hostId tag invalid");
246         &add_content2xml_hash($out_hash, "error", "hostId");
247         &main::daemon_log("$session_id ERROR: no hostId specified or hostId tag invalid: $msg", 1); 
248     }
249     if ((not exists $msg_hash->{'macaddress'}) || (@{$msg_hash->{'macaddress'}} != 1) || (@{$msg_hash->{'macaddress'}}[0] eq ref 'HASH'))  {
250         $error++;
251         &add_content2xml_hash($out_hash, "error_string", "no macaddress specified or macaddress tag invalid");
252         &add_content2xml_hash($out_hash, "error", "macaddress");
253         &main::daemon_log("$session_id ERROR: no macaddress specified or macaddress tag invalid: $msg", 1); 
254     }
256     if (not $error) {
257         # Get hostId
258         $hostId = @{$msg_hash->{'hostId'}}[0];
259         &add_content2xml_hash($out_hash, "hostId", $hostId);
261         # Get macaddress
262         $mac = @{$msg_hash->{'macaddress'}}[0];
263         &add_content2xml_hash($out_hash, "macaddress", $mac);
265         my $name= $hostId;
266         $name=~ s/^([^.]+).*$/$1/;
267         my $domain= $hostId;
268         $domain=~ s/^[^.]+\.(.*)$/$1/;
269         my ($description, $notes, $ip);
271         if (defined @{$msg_hash->{'description'}}[0]){
272             $description = @{$msg_hash->{'description'}}[0];
273         }
274         if (defined @{$msg_hash->{'notes'}}[0]){
275             $notes = @{$msg_hash->{'notes'}}[0];
276         }
277         if (defined @{$msg_hash->{'ip'}}[0]){
278             $ip = @{$msg_hash->{'ip'}}[0];
279         }
281         my $callobj;
282         $callobj = {
283             method  => 'createClient',
284             params  => [ $name, $domain, $description, $notes, $ip, $mac ],
285             id  => 1,
286         };
288         $sres = $main::opsi_client->call($main::opsi_url, $callobj);
289         ($sres_err, $sres_err_string) = &check_opsi_res($sres);
290         if ($sres_err){
291             &main::daemon_log("$session_id ERROR: cannot create client: ".$sres_err_string, 1);
292             &add_content2xml_hash($out_hash, "error", $sres_err_string);
293         } else {
294             &main::daemon_log("$session_id INFO: add opsi client '$hostId' with mac '$mac'", 5); 
295         }
296     }
298     # Return message
299     return ( &create_xml_string($out_hash) );
302 ## @method opsi_modify_client
303 # Modifies the parameters description, mac or notes for an Opsi client if the corresponding message tags are given.
304 # @param msg - STRING - xml message with tag hostId and optional description, mac or notes
305 # @param msg_hash - HASHREF - message information parsed into a hash
306 # @param session_id - INTEGER - POE session id of the processing of this message    
307 # @return out_msg - STRING - feedback to GOsa in success and error case
308 sub opsi_modify_client {
309     my ($msg, $msg_hash, $session_id) = @_;
310     my $header = @{$msg_hash->{'header'}}[0];
311     my $source = @{$msg_hash->{'source'}}[0];
312     my $target = @{$msg_hash->{'target'}}[0];
313     my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
314     my $hostId;
315     my $error = 0;
316     my ($sres, $sres_err, $sres_err_string);
318     # Build return message with twisted target and source
319     my $out_hash = &main::create_xml_hash("answer_$header", $main::server_address, $source);
320     if (defined $forward_to_gosa) {
321         &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
322     }
324     # Sanity check of needed parameter
325     if ((not exists $msg_hash->{'hostId'}) || (@{$msg_hash->{'hostId'}} != 1) || (@{$msg_hash->{'hostId'}}[0] eq ref 'HASH'))  {
326         $error++;
327         &add_content2xml_hash($out_hash, "error_string", "no hostId specified or hostId tag invalid");
328         &add_content2xml_hash($out_hash, "error", "hostId");
329         &main::daemon_log("$session_id ERROR: no hostId specified or hostId tag invalid: $msg", 1); 
330     }
332     if (not $error) {
333         # Get hostId
334         $hostId = @{$msg_hash->{'hostId'}}[0];
335         &add_content2xml_hash($out_hash, "hostId", $hostId);
336         my $name= $hostId;
337         $name=~ s/^([^.]+).*$/$1/;
338         my $domain= $hostId;
339         $domain=~ s/^[^.]+(.*)$/$1/;
341         # Modify description, notes or mac if defined
342         my ($description, $notes, $mac);
343         my $callobj;
344         if ((exists $msg_hash->{'description'}) && (@{$msg_hash->{'description'}} == 1) ){
345             $description = @{$msg_hash->{'description'}}[0];
346             $callobj = {
347                 method  => 'setHostDescription',
348                 params  => [ $hostId, $description ],
349                 id  => 1,
350             };
351             my $sres = $main::opsi_client->call($main::opsi_url, $callobj);
352             my ($sres_err, $sres_err_string) = &check_opsi_res($sres);
353             if ($sres_err){
354                 &main::daemon_log("ERROR: cannot set description: ".$sres_err_string, 1);
355                 &add_content2xml_hash($out_hash, "error", $sres_err_string);
356             }
357         }
358         if ((exists $msg_hash->{'notes'}) && (@{$msg_hash->{'notes'}} == 1)) {
359             $notes = @{$msg_hash->{'notes'}}[0];
360             $callobj = {
361                 method  => 'setHostNotes',
362                 params  => [ $hostId, $notes ],
363                 id  => 1,
364             };
365             my $sres = $main::opsi_client->call($main::opsi_url, $callobj);
366             my ($sres_err, $sres_err_string) = &check_opsi_res($sres);
367             if ($sres_err){
368                 &main::daemon_log("ERROR: cannot set notes: ".$sres_err_string, 1);
369                 &add_content2xml_hash($out_hash, "error", $sres_err_string);
370             }
371         }
372         if ((exists $msg_hash->{'mac'}) && (@{$msg_hash->{'mac'}} == 1)){
373             $mac = @{$msg_hash->{'mac'}}[0];
374             $callobj = {
375                 method  => 'setMacAddress',
376                 params  => [ $hostId, $mac ],
377                 id  => 1,
378             };
379             my $sres = $main::opsi_client->call($main::opsi_url, $callobj);
380             my ($sres_err, $sres_err_string) = &check_opsi_res($sres);
381             if ($sres_err){
382                 &main::daemon_log("ERROR: cannot set mac address: ".$sres_err_string, 1);
383                 &add_content2xml_hash($out_hash, "error", $sres_err_string);
384             }
385         }
386     }
388     # Return message
389     return ( &create_xml_string($out_hash) );
392     
393 ## @method opsi_get_netboot_products
394 # Get netboot products for specific host.
395 # @param msg - STRING - xml message with tag hostId
396 # @param msg_hash - HASHREF - message information parsed into a hash
397 # @param session_id - INTEGER - POE session id of the processing of this message
398 # @return out_msg - STRING - feedback to GOsa in success and error case
399 sub opsi_get_netboot_products {
400     my ($msg, $msg_hash, $session_id) = @_;
401     my $header = @{$msg_hash->{'header'}}[0];
402     my $source = @{$msg_hash->{'source'}}[0];
403     my $target = @{$msg_hash->{'target'}}[0];
404     my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
405     my $hostId;
406     my $xml_msg;
408     # Build return message with twisted target and source
409     my $out_hash = &main::create_xml_hash("answer_$header", $main::server_address, $source);
410     if (defined $forward_to_gosa) {
411         &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
412     }
414     # Get hostId if defined
415     if ((exists $msg_hash->{'hostId'}) && (@{$msg_hash->{'hostId'}} == 1))  {
416         $hostId = @{$msg_hash->{'hostId'}}[0];
417         &add_content2xml_hash($out_hash, "hostId", $hostId);
418     }
420     &add_content2xml_hash($out_hash, "xxx", "");
421     $xml_msg = &create_xml_string($out_hash);
422     # For hosts, only return the products that are or get installed
423     my $callobj;
424     $callobj = {
425         method  => 'getNetBootProductIds_list',
426         params  => [ ],
427         id  => 1,
428     };
429     &main::daemon_log("$session_id DEBUG: send callobj to opsi_client: ".&opsi_callobj2string($callobj), 7);
430     &main::daemon_log("$session_id DEBUG: opsi_url $main::opsi_url", 7);
431     &main::daemon_log("$session_id DEBUG: waiting for answer from opsi_client!", 7);
432     my $res = $main::opsi_client->call($main::opsi_url, $callobj);
433     &main::daemon_log("$session_id DEBUG: get answer from opsi_client", 7);
434     my %r = ();
435     for (@{$res->result}) { $r{$_} = 1 }
437     if (not &check_opsi_res($res)){
439         if (defined $hostId){
441             $callobj = {
442                 method  => 'getProductStates_hash',
443                 params  => [ $hostId ],
444                 id  => 1,
445             };
447             my $hres = $main::opsi_client->call($main::opsi_url, $callobj);
448             if (not &check_opsi_res($hres)){
449                 my $htmp= $hres->result->{$hostId};
451                 # check state != not_installed or action == setup -> load and add
452                 foreach my $product (@{$htmp}){
454                     if (!defined ($r{$product->{'productId'}})){
455                         next;
456                     }
458                     # Now we've a couple of hashes...
459                     if ($product->{'installationStatus'} ne "not_installed" or
460                             $product->{'actionRequest'} eq "setup"){
461                         my $state= "<state>".$product->{'installationStatus'}."</state><action>".$product->{'actionRequest'}."</action>";
463                         $callobj = {
464                             method  => 'getProduct_hash',
465                             params  => [ $product->{'productId'} ],
466                             id  => 1,
467                         };
469                         my $sres = $main::opsi_client->call($main::opsi_url, $callobj);
470                         if (not &check_opsi_res($sres)){
471                             my $tres= $sres->result;
473                             my $name= xml_quote($tres->{'name'});
474                             my $r= $product->{'productId'};
475                             my $description= xml_quote($tres->{'description'});
476                             $name=~ s/\//\\\//;
477                             $description=~ s/\//\\\//;
478                             $xml_msg=~ s/<xxx><\/xxx>/\n<item><productId>$r<\/productId><name><\/name><description>$description<\/description><\/item>$state<xxx><\/xxx>/;
479                         }
480                     }
481                 }
483             }
485         } else {
486             foreach my $r (@{$res->result}) {
487                 $callobj = {
488                     method  => 'getProduct_hash',
489                     params  => [ $r ],
490                     id  => 1,
491                 };
493                 my $sres = $main::opsi_client->call($main::opsi_url, $callobj);
494                 if (not &check_opsi_res($sres)){
495                     my $tres= $sres->result;
497                     my $name= xml_quote($tres->{'name'});
498                     my $description= xml_quote($tres->{'description'});
499                     $name=~ s/\//\\\//;
500                     $description=~ s/\//\\\//;
501                     $xml_msg=~ s/<xxx><\/xxx>/\n<item><productId>$r<\/productId><name><\/name><description>$description<\/description><\/item><xxx><\/xxx>/;
502                 }
503             }
505         }
506     }
507     $xml_msg=~ s/<xxx><\/xxx>//;
509     # Return message
510     return ( $xml_msg );
514 ## @method opsi_get_product_properties
515 # Get product properties for a product and a specific host or gobally for a product.
516 # @param msg - STRING - xml message with tags productId and optional hostId
517 # @param msg_hash - HASHREF - message information parsed into a hash
518 # @param session_id - INTEGER - POE session id of the processing of this message
519 # @return out_msg - STRING - feedback to GOsa in success and error case
520 sub opsi_get_product_properties {
521     my ($msg, $msg_hash, $session_id) = @_;
522     my $header = @{$msg_hash->{'header'}}[0];
523     my $source = @{$msg_hash->{'source'}}[0];
524     my $target = @{$msg_hash->{'target'}}[0];
525     my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
526     my ($hostId, $productId);
527     my $xml_msg;
529     # Build return message with twisted target and source
530     my $out_hash = &main::create_xml_hash("answer_$header", $main::server_address, $source);
531     if (defined $forward_to_gosa) {
532         &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
533     }
535     # Sanity check of needed parameter
536     if ((not exists $msg_hash->{'productId'}) || (@{$msg_hash->{'productId'}} != 1) || (@{$msg_hash->{'productId'}}[0] eq ref 'HASH'))  {
537         &add_content2xml_hash($out_hash, "error_string", "no productId specified or productId tag invalid");
538         &add_content2xml_hash($out_hash, "error", "productId");
539         &main::daemon_log("$session_id ERROR: no productId specified or productId tag invalid: $msg", 1); 
541         # Return message
542         return ( &create_xml_string($out_hash) );
543     }
545     # Get productid
546     $productId = @{$msg_hash->{'productId'}}[0];
547     &add_content2xml_hash($out_hash, "producId", "$productId");
549     # Get hostId if defined
550     if (defined @{$msg_hash->{'hostId'}}[0]){
551       $hostId = @{$msg_hash->{'hostId'}}[0];
552       &add_content2xml_hash($out_hash, "hostId", $hostId);
553     }
555     # Load actions
556     my $callobj = {
557       method  => 'getPossibleProductActions_list',
558       params  => [ $productId ],
559       id  => 1,
560     };
561     my $res = $main::opsi_client->call($main::opsi_url, $callobj);
562     if (not &check_opsi_res($res)){
563       foreach my $action (@{$res->result}){
564         &add_content2xml_hash($out_hash, "action", $action);
565       }
566     }
568     # Add place holder
569     &add_content2xml_hash($out_hash, "xxx", "");
571     # Move to XML string
572     $xml_msg= &create_xml_string($out_hash);
574     # JSON Query
575     if (defined $hostId){
576       $callobj = {
577           method  => 'getProductProperties_hash',
578           params  => [ $productId, $hostId ],
579           id  => 1,
580       };
581     } else {
582       $callobj = {
583           method  => 'getProductProperties_hash',
584           params  => [ $productId ],
585           id  => 1,
586       };
587     }
588     $res = $main::opsi_client->call($main::opsi_url, $callobj);
590     # JSON Query 2
591     $callobj = {
592       method  => 'getProductPropertyDefinitions_listOfHashes',
593       params  => [ $productId ],
594       id  => 1,
595     };
597     # Assemble options
598     my $res2 = $main::opsi_client->call($main::opsi_url, $callobj);
599     my $values = {};
600     my $descriptions = {};
601     if (not &check_opsi_res($res2)){
602         my $r= $res2->result;
604           foreach my $entr (@$r){
605             # Unroll values
606             my $cnv;
607             if (UNIVERSAL::isa( $entr->{'values'}, "ARRAY" )){
608               foreach my $v (@{$entr->{'values'}}){
609                 $cnv.= "<value>$v</value>";
610               }
611             } else {
612               $cnv= $entr->{'values'};
613             }
614             $values->{$entr->{'name'}}= $cnv;
615             $descriptions->{$entr->{'name'}}= "<description>".$entr->{'description'}."</description>";
616           }
617     }
619     if (not &check_opsi_res($res)){
620         my $r= $res->result;
621         foreach my $key (keys %{$r}) {
622             my $item= "\n<item>";
623             my $value= $r->{$key};
624             my $dsc= "";
625             my $vals= "";
626             if (defined $descriptions->{$key}){
627               $dsc= $descriptions->{$key};
628             }
629             if (defined $values->{$key}){
630               $vals= $values->{$key};
631             }
632             $item.= "<$key>$dsc<default>".xml_quote($value)."</default>$vals</$key>";
633             $item.= "</item>";
634             $xml_msg=~ s/<xxx><\/xxx>/$item<xxx><\/xxx>/;
635         }
636     }
638     $xml_msg=~ s/<xxx><\/xxx>//;
640     # Return message
641     return ( $xml_msg );
645 ## @method opsi_set_product_properties
646 # Set product properities for a specific host or globaly. Message needs one xml tag 'item' and within one xml tag 'name' and 'value'. The xml tags action and state are optional.
647 # @param msg - STRING - xml message with tags productId, action, state and optional hostId, action and state
648 # @param msg_hash - HASHREF - message information parsed into a hash
649 # @param session_id - INTEGER - POE session id of the processing of this message
650 # @return out_msg - STRING - feedback to GOsa in success and error case
651 sub opsi_set_product_properties {
652     my ($msg, $msg_hash, $session_id) = @_;
653     my $header = @{$msg_hash->{'header'}}[0];
654     my $source = @{$msg_hash->{'source'}}[0];
655     my $target = @{$msg_hash->{'target'}}[0];
656     my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
657     my ($productId, $hostId);
659     # Build return message with twisted target and source
660     my $out_hash = &main::create_xml_hash("answer_$header", $main::server_address, $source);
661     if (defined $forward_to_gosa) {
662         &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
663     }
665     # Sanity check of needed parameter
666     if ((not exists $msg_hash->{'productId'}) || (@{$msg_hash->{'productId'}} != 1) || (@{$msg_hash->{'productId'}}[0] eq ref 'HASH'))  {
667         &add_content2xml_hash($out_hash, "error_string", "no productId specified or productId tag invalid");
668         &add_content2xml_hash($out_hash, "error", "productId");
669         &main::daemon_log("$session_id ERROR: no productId specified or productId tag invalid: $msg", 1); 
670         return ( &create_xml_string($out_hash) );
671     }
672     if (not exists $msg_hash->{'item'}) {
673         &add_content2xml_hash($out_hash, "error_string", "message needs one xml-tag 'item' and within the xml-tags 'name' and 'value'");
674         &add_content2xml_hash($out_hash, "error", "item");
675         &main::daemon_log("$session_id ERROR: message needs one xml-tag 'item' and within the xml-tags 'name' and 'value': $msg", 1); 
676         return ( &create_xml_string($out_hash) );
677     } else {
678         if ((not exists @{$msg_hash->{'item'}}[0]->{'name'}) || (@{@{$msg_hash->{'item'}}[0]->{'name'}} != 1 )) {
679             &add_content2xml_hash($out_hash, "error_string", "message needs within the xml-tag 'item' one xml-tags 'name'");
680             &add_content2xml_hash($out_hash, "error", "name");
681             &main::daemon_log("$session_id ERROR: message needs within the xml-tag 'item' one xml-tags 'name': $msg", 1); 
682             return ( &create_xml_string($out_hash) );
683         }
684         if ((not exists @{$msg_hash->{'item'}}[0]->{'value'}) || (@{@{$msg_hash->{'item'}}[0]->{'value'}} != 1 )) {
685             &add_content2xml_hash($out_hash, "error_string", "message needs within the xml-tag 'item' one xml-tags 'value'");
686             &add_content2xml_hash($out_hash, "error", "value");
687             &main::daemon_log("$session_id ERROR: message needs within the xml-tag 'item' one xml-tags 'value': $msg", 1); 
688             return ( &create_xml_string($out_hash) );
689         }
690     }
691     # if no hostId is given, set_product_properties will act on globally
692     if ((exists $msg_hash->{'hostId'}) && (@{$msg_hash->{'hostId'}} > 1))  {
693         &add_content2xml_hash($out_hash, "error_string", "hostId contains no or more than one values");
694         &add_content2xml_hash($out_hash, "error", "hostId");
695         &main::daemon_log("$session_id ERROR: hostId contains no or more than one values: $msg", 1); 
696         return ( &create_xml_string($out_hash) );
697     }
699         
700     # Get productId
701     $productId =  @{$msg_hash->{'productId'}}[0];
702     &add_content2xml_hash($out_hash, "productId", $productId);
704     # Get hostId if defined
705     if (exists $msg_hash->{'hostId'}){
706         $hostId = @{$msg_hash->{'hostId'}}[0];
707         &add_content2xml_hash($out_hash, "hostId", $hostId);
708     }
710     # Set product states if requested
711     if (defined @{$msg_hash->{'action'}}[0]){
712         &_set_action($productId, @{$msg_hash->{'action'}}[0], $hostId);
713     }
714     if (defined @{$msg_hash->{'state'}}[0]){
715         &_set_state($productId, @{$msg_hash->{'state'}}[0], $hostId);
716     }
718     # Find properties
719     foreach my $item (@{$msg_hash->{'item'}}){
720         # JSON Query
721         my $callobj;
723         if (defined $hostId){
724             $callobj = {
725                 method  => 'setProductProperty',
726                 params  => [ $productId, $item->{'name'}[0], $item->{'value'}[0], $hostId ],
727                 id  => 1,
728             };
729         } else {
730             $callobj = {
731                 method  => 'setProductProperty',
732                 params  => [ $productId, $item->{'name'}[0], $item->{'value'}[0] ],
733                 id  => 1,
734             };
735         }
737         my $res = $main::opsi_client->call($main::opsi_url, $callobj);
738         my ($res_err, $res_err_string) = &check_opsi_res($res);
740         if ($res_err){
741             &main::daemon_log("$session_id ERROR: communication failed while setting '".$item->{'name'}[0]."': ".$res_err_string, 1);
742             &add_content2xml_hash($out_hash, "error", $res_err_string);
743         }
744     }
747     # Return message
748     return ( &create_xml_string($out_hash) );
752 ## @method opsi_get_client_hardware
753 # Reports client hardware inventory.
754 # @param msg - STRING - xml message with tag hostId
755 # @param msg_hash - HASHREF - message information parsed into a hash
756 # @param session_id - INTEGER - POE session id of the processing of this message
757 # @return out_msg - STRING - feedback to GOsa in success and error case
758 sub opsi_get_client_hardware {
759     my ($msg, $msg_hash, $session_id) = @_;
760     my $header = @{$msg_hash->{'header'}}[0];
761     my $source = @{$msg_hash->{'source'}}[0];
762     my $target = @{$msg_hash->{'target'}}[0];
763     my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
764     my $hostId;
765     my $error = 0;
766     my $xml_msg;
768     # Build return message with twisted target and source
769     my $out_hash = &main::create_xml_hash("answer_$header", $main::server_address, $source);
770     if (defined $forward_to_gosa) {
771       &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
772     }
774     # Sanity check of needed parameter
775     if ((exists $msg_hash->{'hostId'}) && (@{$msg_hash->{'hostId'}} != 1) || (@{$msg_hash->{'hostId'}}[0] eq ref 'HASH'))  {
776         $error++;
777         &add_content2xml_hash($out_hash, "error_string", "hostId contains no or more than one values");
778         &add_content2xml_hash($out_hash, "error", "hostId");
779         &main::daemon_log("$session_id ERROR: hostId contains no or more than one values: $msg", 1); 
780     }
782     if (not $error) {
784     # Get hostId
785         $hostId = @{$msg_hash->{'hostId'}}[0];
786         &add_content2xml_hash($out_hash, "hostId", "$hostId");
787         &add_content2xml_hash($out_hash, "xxx", "");
788     }    
790     # Move to XML string
791     $xml_msg= &create_xml_string($out_hash);
792     
793     if (not $error) {
795     # JSON Query
796         my $callobj = {
797             method  => 'getHardwareInformation_hash',
798             params  => [ $hostId ],
799             id  => 1,
800         };
802         my $res = $main::opsi_client->call($main::opsi_url, $callobj);
803         if (not &check_opsi_res($res)){
804             my $result= $res->result;
805             if (ref $result eq "HASH") {
806                 foreach my $r (keys %{$result}){
807                     my $item= "\n<item><id>".xml_quote($r)."</id>";
808                     my $value= $result->{$r};
809                     foreach my $sres (@{$value}){
811                         foreach my $dres (keys %{$sres}){
812                             if (defined $sres->{$dres}){
813                                 $item.= "<$dres>".xml_quote($sres->{$dres})."</$dres>";
814                             }
815                         }
817                     }
818                     $item.= "</item>";
819                     $xml_msg=~ s%<xxx></xxx>%$item<xxx></xxx>%;
821                 }
822             }
823         }
825         $xml_msg=~ s/<xxx><\/xxx>//;
827     }
829     # Return message
830     return ( $xml_msg );
834 ## @method opsi_list_clients
835 # Reports all Opsi clients. 
836 # @param msg - STRING - xml message 
837 # @param msg_hash - HASHREF - message information parsed into a hash
838 # @param session_id - INTEGER - POE session id of the processing of this message
839 # @return out_msg - STRING - feedback to GOsa in success and error case
840 sub opsi_list_clients {
841     my ($msg, $msg_hash, $session_id) = @_;
842     my $header = @{$msg_hash->{'header'}}[0];
843     my $source = @{$msg_hash->{'source'}}[0];
844     my $target = @{$msg_hash->{'target'}}[0];
845     my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
847     # Build return message with twisted target and source
848     my $out_hash = &main::create_xml_hash("answer_$header", $main::server_address, $source);
849     if (defined $forward_to_gosa) {
850       &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
851     }
852     &add_content2xml_hash($out_hash, "xxx", "");
854     # Move to XML string
855     my $xml_msg= &create_xml_string($out_hash);
857     # JSON Query
858     my $callobj = {
859         method  => 'getClients_listOfHashes',
860         params  => [ ],
861         id  => 1,
862     };
863     my $res = $main::opsi_client->call($main::opsi_url, $callobj);
864     if (not &check_opsi_res($res)){
865         foreach my $host (@{$res->result}){
866             my $item= "\n<item><name>".$host->{'hostId'}."</name>";
867             if (defined($host->{'description'})){
868                 $item.= "<description>".xml_quote($host->{'description'})."</description>";
869             }
870             if (defined($host->{'notes'})){
871                 $item.= "<notes>".xml_quote($host->{'notes'})."</notes>";
872             }
873             if (defined($host->{'lastSeen'})){
874                 $item.= "<lastSeen>".xml_quote($host->{'lastSeen'})."</lastSeen>";
875             }
877             $callobj = {
878               method  => 'getIpAddress',
879               params  => [ $host->{'hostId'} ],
880               id  => 1,
881             };
882             my $sres= $main::opsi_client->call($main::opsi_url, $callobj);
883             if ( not &check_opsi_res($sres)){
884               $item.= "<ip>".xml_quote($sres->result)."</ip>";
885             }
887             $callobj = {
888               method  => 'getMacAddress',
889               params  => [ $host->{'hostId'} ],
890               id  => 1,
891             };
892             $sres= $main::opsi_client->call($main::opsi_url, $callobj);
893             if ( not &check_opsi_res($sres)){
894                 $item.= "<mac>".xml_quote($sres->result)."</mac>";
895             }
896             $item.= "</item>";
897             $xml_msg=~ s%<xxx></xxx>%$item<xxx></xxx>%;
898         }
899     }
901     $xml_msg=~ s/<xxx><\/xxx>//;
902     return ( $xml_msg );
907 ## @method opsi_get_client_software
908 # Reports client software inventory.
909 # @param msg - STRING - xml message with tag hostId
910 # @param msg_hash - HASHREF - message information parsed into a hash
911 # @param session_id - INTEGER - POE session id of the processing of this message
912 # @return out_msg - STRING - feedback to GOsa in success and error case
913 sub opsi_get_client_software {
914     my ($msg, $msg_hash, $session_id) = @_;
915     my $header = @{$msg_hash->{'header'}}[0];
916     my $source = @{$msg_hash->{'source'}}[0];
917     my $target = @{$msg_hash->{'target'}}[0];
918     my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
919     my $error = 0;
920     my $hostId;
921     my $xml_msg;
923     # Build return message with twisted target and source
924     my $out_hash = &main::create_xml_hash("answer_$header", $main::server_address, $source);
925     if (defined $forward_to_gosa) {
926       &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
927     }
929     # Sanity check of needed parameter
930     if ((exists $msg_hash->{'hostId'}) && (@{$msg_hash->{'hostId'}} != 1) || (@{$msg_hash->{'hostId'}}[0] eq ref 'HASH'))  {
931         $error++;
932         &add_content2xml_hash($out_hash, "error_string", "hostId contains no or more than one values");
933         &add_content2xml_hash($out_hash, "error", "hostId");
934         &main::daemon_log("$session_id ERROR: hostId contains no or more than one values: $msg", 1); 
935     }
937     if (not $error) {
939     # Get hostId
940         $hostId = @{$msg_hash->{'hostId'}}[0];
941         &add_content2xml_hash($out_hash, "hostId", "$hostId");
942         &add_content2xml_hash($out_hash, "xxx", "");
943     }
945     $xml_msg= &create_xml_string($out_hash);
947     if (not $error) {
949     # JSON Query
950         my $callobj = {
951             method  => 'getSoftwareInformation_hash',
952             params  => [ $hostId ],
953             id  => 1,
954         };
956         my $res = $main::opsi_client->call($main::opsi_url, $callobj);
957         if (not &check_opsi_res($res)){
958             my $result= $res->result;
959 # TODO : Ist das hier schon fertig???   
960         }
962         $xml_msg=~ s/<xxx><\/xxx>//;
964     }
966     # Return message
967     return ( $xml_msg );
971 ## @method opsi_get_local_products
972 # Reports product for given hostId or globally.
973 # @param msg - STRING - xml message with optional tag hostId
974 # @param msg_hash - HASHREF - message information parsed into a hash
975 # @param session_id - INTEGER - POE session id of the processing of this message
976 # @return out_msg - STRING - feedback to GOsa in success and error case
977 sub opsi_get_local_products {
978     my ($msg, $msg_hash, $session_id) = @_;
979     my $header = @{$msg_hash->{'header'}}[0];
980     my $source = @{$msg_hash->{'source'}}[0];
981     my $target = @{$msg_hash->{'target'}}[0];
982     my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
983     my $hostId;
985     # Build return message with twisted target and source
986     my $out_hash = &main::create_xml_hash("answer_$header", $main::server_address, $source);
987     if (defined $forward_to_gosa) {
988         &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
989     }
990     &add_content2xml_hash($out_hash, "xxx", "");
992     # Get hostId if defined
993     if ((exists $msg_hash->{'hostId'}) && (@{$msg_hash->{'hostId'}} == 1))  {
994         $hostId = @{$msg_hash->{'hostId'}}[0];
995         &add_content2xml_hash($out_hash, "hostId", $hostId);
996     }
998     # Move to XML string
999     my $xml_msg= &create_xml_string($out_hash);
1001     # For hosts, only return the products that are or get installed
1002     my $callobj;
1003     $callobj = {
1004         method  => 'getLocalBootProductIds_list',
1005         params  => [ ],
1006         id  => 1,
1007     };
1009     my $res = $main::opsi_client->call($main::opsi_url, $callobj);
1010     my %r = ();
1011     for (@{$res->result}) { $r{$_} = 1 }
1013     if (not &check_opsi_res($res)){
1015         if (defined $hostId){
1016             $callobj = {
1017                 method  => 'getProductStates_hash',
1018                 params  => [ $hostId ],
1019                 id  => 1,
1020             };
1022             my $hres = $main::opsi_client->call($main::opsi_url, $callobj);
1023             if (not &check_opsi_res($hres)){
1024                 my $htmp= $hres->result->{$hostId};
1026                 # Check state != not_installed or action == setup -> load and add
1027                 foreach my $product (@{$htmp}){
1029                     if (!defined ($r{$product->{'productId'}})){
1030                         next;
1031                     }
1033                     # Now we've a couple of hashes...
1034                     if ($product->{'installationStatus'} ne "not_installed" or
1035                             $product->{'actionRequest'} eq "setup"){
1036                         my $state= "<state>".$product->{'installationStatus'}."</state><action>".$product->{'actionRequest'}."</action>";
1038                         $callobj = {
1039                             method  => 'getProduct_hash',
1040                             params  => [ $product->{'productId'} ],
1041                             id  => 1,
1042                         };
1044                         my $sres = $main::opsi_client->call($main::opsi_url, $callobj);
1045                         if (not &check_opsi_res($sres)){
1046                             my $tres= $sres->result;
1048                             my $name= xml_quote($tres->{'name'});
1049                             my $r= $product->{'productId'};
1050                             my $description= xml_quote($tres->{'description'});
1051                             $name=~ s/\//\\\//;
1052                             $description=~ s/\//\\\//;
1053                             $xml_msg=~ s/<xxx><\/xxx>/\n<item><productId>$r<\/productId><name><\/name><description>$description<\/description><\/item>$state<xxx><\/xxx>/;
1054                         }
1056                     }
1057                 }
1059             }
1061         } else {
1062             foreach my $r (@{$res->result}) {
1063                 $callobj = {
1064                     method  => 'getProduct_hash',
1065                     params  => [ $r ],
1066                     id  => 1,
1067                 };
1069                 my $sres = $main::opsi_client->call($main::opsi_url, $callobj);
1070                 if (not &check_opsi_res($sres)){
1071                     my $tres= $sres->result;
1073                     my $name= xml_quote($tres->{'name'});
1074                     my $description= xml_quote($tres->{'description'});
1075                     $name=~ s/\//\\\//;
1076                     $description=~ s/\//\\\//;
1077                     $xml_msg=~ s/<xxx><\/xxx>/\n<item><productId>$r<\/productId><name><\/name><description>$description<\/description><\/item><xxx><\/xxx>/;
1078                 }
1080             }
1082         }
1083     }
1085     $xml_msg=~ s/<xxx><\/xxx>//;
1087     # Retrun Message
1088     return ( $xml_msg );
1092 ## @method opsi_del_client
1093 # Deletes a client from Opsi.
1094 # @param msg - STRING - xml message with tag hostId
1095 # @param msg_hash - HASHREF - message information parsed into a hash
1096 # @param session_id - INTEGER - POE session id of the processing of this message
1097 # @return out_msg - STRING - feedback to GOsa in success and error case
1098 sub opsi_del_client {
1099     my ($msg, $msg_hash, $session_id) = @_;
1100     my $header = @{$msg_hash->{'header'}}[0];
1101     my $source = @{$msg_hash->{'source'}}[0];
1102     my $target = @{$msg_hash->{'target'}}[0];
1103     my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
1104     my $hostId;
1105     my $error = 0;
1107     # Build return message with twisted target and source
1108     my $out_hash = &main::create_xml_hash("answer_$header", $main::server_address, $source);
1109     if (defined $forward_to_gosa) {
1110       &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
1111     }
1113     # Sanity check of needed parameter
1114     if ((exists $msg_hash->{'hostId'}) && (@{$msg_hash->{'hostId'}} != 1) || (@{$msg_hash->{'hostId'}}[0] eq ref 'HASH'))  {
1115         $error++;
1116         &add_content2xml_hash($out_hash, "error_string", "hostId contains no or more than one values");
1117         &add_content2xml_hash($out_hash, "error", "hostId");
1118         &main::daemon_log("$session_id ERROR: hostId contains no or more than one values: $msg", 1); 
1119     }
1121     if (not $error) {
1123     # Get hostId
1124         $hostId = @{$msg_hash->{'hostId'}}[0];
1125         &add_content2xml_hash($out_hash, "hostId", "$hostId");
1127     # JSON Query
1128         my $callobj = {
1129             method  => 'deleteClient',
1130             params  => [ $hostId ],
1131             id  => 1,
1132         };
1133         my $res = $main::opsi_client->call($main::opsi_url, $callobj);
1134     }
1136     # Move to XML string
1137     my $xml_msg= &create_xml_string($out_hash);
1139     # Return message
1140     return ( $xml_msg );
1144 ## @method opsi_install_client
1145 # Set a client in Opsi to install and trigger a wake on lan message (WOL).  
1146 # @param msg - STRING - xml message with tags hostId, macaddress
1147 # @param msg_hash - HASHREF - message information parsed into a hash
1148 # @param session_id - INTEGER - POE session id of the processing of this message
1149 # @return out_msg - STRING - feedback to GOsa in success and error case
1150 sub opsi_install_client {
1151     my ($msg, $msg_hash, $session_id) = @_;
1152     my $header = @{$msg_hash->{'header'}}[0];
1153     my $source = @{$msg_hash->{'source'}}[0];
1154     my $target = @{$msg_hash->{'target'}}[0];
1155     my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
1158     my ($hostId, $macaddress);
1160     my $error = 0;
1161     my @out_msg_l;
1163     # Build return message with twisted target and source
1164     my $out_hash = &main::create_xml_hash("answer_$header", $main::server_address, $source);
1165     if (defined $forward_to_gosa) {
1166         &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
1167     }
1169     # Sanity check of needed parameter
1170     if ((not exists $msg_hash->{'hostId'}) || (@{$msg_hash->{'hostId'}} != 1) || (@{$msg_hash->{'hostId'}}[0] eq ref 'HASH'))  {
1171         $error++;
1172         &add_content2xml_hash($out_hash, "error_string", "no hostId specified or hostId tag invalid");
1173         &add_content2xml_hash($out_hash, "error", "hostId");
1174         &main::daemon_log("$session_id ERROR: no hostId specified or hostId tag invalid: $msg", 1); 
1175     }
1176     if ((not exists $msg_hash->{'macaddress'}) || (@{$msg_hash->{'macaddress'}} != 1) || (@{$msg_hash->{'macaddress'}}[0] eq ref 'HASH') )  {
1177         $error++;
1178         &add_content2xml_hash($out_hash, "error_string", "no macaddress specified or macaddress tag invalid");
1179         &add_content2xml_hash($out_hash, "error", "macaddress");
1180         &main::daemon_log("$session_id ERROR: no macaddress specified or macaddress tag invalid: $msg", 1); 
1181     } else {
1182         if ((exists $msg_hash->{'macaddress'}) && 
1183                 ($msg_hash->{'macaddress'}[0] =~ /^([0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2})$/i)) {  
1184             $macaddress = $1; 
1185         } else { 
1186             $error ++; 
1187             &add_content2xml_hash($out_hash, "error_string", "given mac address is not correct");
1188             &add_content2xml_hash($out_hash, "error", "macaddress");
1189             &main::daemon_log("$session_id ERROR: given mac address is not correct: $msg", 1); 
1190         }
1191     }
1193     if (not $error) {
1195     # Get hostId
1196         $hostId = @{$msg_hash->{'hostId'}}[0];
1197         &add_content2xml_hash($out_hash, "hostId", "$hostId");
1199         # Load all products for this host with status != "not_installed" or actionRequest != "none"
1200         my $callobj = {
1201             method  => 'getProductStates_hash',
1202             params  => [ $hostId ],
1203             id  => 1,
1204         };
1206         my $hres = $main::opsi_client->call($main::opsi_url, $callobj);
1207         if (not &check_opsi_res($hres)){
1208             my $htmp= $hres->result->{$hostId};
1210             # check state != not_installed or action == setup -> load and add
1211             foreach my $product (@{$htmp}){
1212                 # Now we've a couple of hashes...
1213                 if ($product->{'installationStatus'} ne "not_installed" or
1214                         $product->{'actionRequest'} ne "none"){
1216                     # Do an action request for all these -> "setup".
1217                     $callobj = {
1218                         method  => 'setProductActionRequest',
1219                         params  => [ $product->{'productId'}, $hostId, "setup" ],
1220                         id  => 1,
1221                     };
1222                     my $res = $main::opsi_client->call($main::opsi_url, $callobj);
1223                     my ($res_err, $res_err_string) = &check_opsi_res($res);
1224                     if ($res_err){
1225                         &main::daemon_log("$session_id ERROR: cannot set product action request for '$hostId': ".$product->{'productId'}, 1);
1226                     } else {
1227                         &main::daemon_log("$session_id INFO: requesting 'setup' for '".$product->{'productId'}."' on $hostId", 1);
1228                     }
1229                 }
1230             }
1231         }
1232         push(@out_msg_l, &create_xml_string($out_hash));
1233     
1235     # Build wakeup message for client
1236         if (not $error) {
1237             my $wakeup_hash = &create_xml_hash("trigger_wake", "GOSA", "KNOWN_SERVER");
1238             &add_content2xml_hash($wakeup_hash, 'macaddress', $macaddress);
1239             my $wakeup_msg = &create_xml_string($wakeup_hash);
1240             push(@out_msg_l, $wakeup_msg);
1242             # invoke trigger wake for this gosa-si-server
1243             &main::server_server_com::trigger_wake($wakeup_msg, $wakeup_hash, $session_id);
1244         }
1245     }
1246     
1247     # Return messages
1248     return @out_msg_l;
1252 ## @method _set_action
1253 # Set action for an Opsi client
1254 # @param product - STRING - Opsi product
1255 # @param action - STRING - action
1256 # @param hostId - STRING - Opsi hostId
1257 sub _set_action {
1258   my $product= shift;
1259   my $action = shift;
1260   my $hostId = shift;
1261   my $callobj;
1263   $callobj = {
1264     method  => 'setProductActionRequest',
1265     params  => [ $product, $hostId, $action],
1266     id  => 1,
1267   };
1269   $main::opsi_client->call($main::opsi_url, $callobj);
1272 ## @method _set_state
1273 # Set state for an Opsi client
1274 # @param product - STRING - Opsi product
1275 # @param action - STRING - state
1276 # @param hostId - STRING - Opsi hostId
1277 sub _set_state {
1278   my $product = shift;
1279   my $state = shift;
1280   my $hostId = shift;
1281   my $callobj;
1283   $callobj = {
1284     method  => 'setProductState',
1285     params  => [ $product, $hostId, $state ],
1286     id  => 1,
1287   };
1289   $main::opsi_client->call($main::opsi_url, $callobj);
1292 1;