Code

* delete debug lines for opsi_get_netboot_product problem
[gosa.git] / 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     };
430     &main::daemon_log("$session_id DEBUG: send callobj to opsi_client: ".&opsi_callobj2string($callobj), 7);
431     my $res = $main::opsi_client->call($main::opsi_url, $callobj);
432     &main::daemon_log("$session_id DEBUG: get answer from opsi_client with number of entries: ".length(@$res), 7);
433     my %r = ();
434     for (@{$res->result}) { $r{$_} = 1 }
436     if (not &check_opsi_res($res)){
438         if (defined $hostId){
440             $callobj = {
441                 method  => 'getProductStates_hash',
442                 params  => [ $hostId ],
443                 id  => 1,
444             };
446             my $hres = $main::opsi_client->call($main::opsi_url, $callobj);
447             if (not &check_opsi_res($hres)){
448                 my $htmp= $hres->result->{$hostId};
450                 # check state != not_installed or action == setup -> load and add
451                 foreach my $product (@{$htmp}){
453                     if (!defined ($r{$product->{'productId'}})){
454                         next;
455                     }
457                     # Now we've a couple of hashes...
458                     if ($product->{'installationStatus'} ne "not_installed" or
459                             $product->{'actionRequest'} eq "setup"){
460                         my $state= "<state>".$product->{'installationStatus'}."</state><action>".$product->{'actionRequest'}."</action>";
462                         $callobj = {
463                             method  => 'getProduct_hash',
464                             params  => [ $product->{'productId'} ],
465                             id  => 1,
466                         };
468                         my $sres = $main::opsi_client->call($main::opsi_url, $callobj);
469                         if (not &check_opsi_res($sres)){
470                             my $tres= $sres->result;
472                             my $name= xml_quote($tres->{'name'});
473                             my $r= $product->{'productId'};
474                             my $description= xml_quote($tres->{'description'});
475                             $name=~ s/\//\\\//;
476                             $description=~ s/\//\\\//;
477                             $xml_msg=~ s/<xxx><\/xxx>/\n<item><productId>$r<\/productId><name><\/name><description>$description<\/description><\/item>$state<xxx><\/xxx>/;
478                         }
479                     }
480                 }
482             }
484         } else {
485             foreach my $r (@{$res->result}) {
486                 $callobj = {
487                     method  => 'getProduct_hash',
488                     params  => [ $r ],
489                     id  => 1,
490                 };
492                 my $sres = $main::opsi_client->call($main::opsi_url, $callobj);
493                 if (not &check_opsi_res($sres)){
494                     my $tres= $sres->result;
496                     my $name= xml_quote($tres->{'name'});
497                     my $description= xml_quote($tres->{'description'});
498                     $name=~ s/\//\\\//;
499                     $description=~ s/\//\\\//;
500                     $xml_msg=~ s/<xxx><\/xxx>/\n<item><productId>$r<\/productId><name><\/name><description>$description<\/description><\/item><xxx><\/xxx>/;
501                 }
502             }
504         }
505     }
506     $xml_msg=~ s/<xxx><\/xxx>//;
508     # Return message
509     return ( $xml_msg );
513 ## @method opsi_get_product_properties
514 # Get product properties for a product and a specific host or gobally for a product.
515 # @param msg - STRING - xml message with tags productId and optional hostId
516 # @param msg_hash - HASHREF - message information parsed into a hash
517 # @param session_id - INTEGER - POE session id of the processing of this message
518 # @return out_msg - STRING - feedback to GOsa in success and error case
519 sub opsi_get_product_properties {
520     my ($msg, $msg_hash, $session_id) = @_;
521     my $header = @{$msg_hash->{'header'}}[0];
522     my $source = @{$msg_hash->{'source'}}[0];
523     my $target = @{$msg_hash->{'target'}}[0];
524     my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
525     my ($hostId, $productId);
526     my $xml_msg;
528     # Build return message with twisted target and source
529     my $out_hash = &main::create_xml_hash("answer_$header", $main::server_address, $source);
530     if (defined $forward_to_gosa) {
531         &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
532     }
534     # Sanity check of needed parameter
535     if ((not exists $msg_hash->{'productId'}) || (@{$msg_hash->{'productId'}} != 1) || (@{$msg_hash->{'productId'}}[0] eq ref 'HASH'))  {
536         &add_content2xml_hash($out_hash, "error_string", "no productId specified or productId tag invalid");
537         &add_content2xml_hash($out_hash, "error", "productId");
538         &main::daemon_log("$session_id ERROR: no productId specified or productId tag invalid: $msg", 1); 
540         # Return message
541         return ( &create_xml_string($out_hash) );
542     }
544     # Get productid
545     $productId = @{$msg_hash->{'productId'}}[0];
546     &add_content2xml_hash($out_hash, "producId", "$productId");
548     # Get hostId if defined
549     if (defined @{$msg_hash->{'hostId'}}[0]){
550       $hostId = @{$msg_hash->{'hostId'}}[0];
551       &add_content2xml_hash($out_hash, "hostId", $hostId);
552     }
554     # Load actions
555     my $callobj = {
556       method  => 'getPossibleProductActions_list',
557       params  => [ $productId ],
558       id  => 1,
559     };
560     my $res = $main::opsi_client->call($main::opsi_url, $callobj);
561     if (not &check_opsi_res($res)){
562       foreach my $action (@{$res->result}){
563         &add_content2xml_hash($out_hash, "action", $action);
564       }
565     }
567     # Add place holder
568     &add_content2xml_hash($out_hash, "xxx", "");
570     # Move to XML string
571     $xml_msg= &create_xml_string($out_hash);
573     # JSON Query
574     if (defined $hostId){
575       $callobj = {
576           method  => 'getProductProperties_hash',
577           params  => [ $productId, $hostId ],
578           id  => 1,
579       };
580     } else {
581       $callobj = {
582           method  => 'getProductProperties_hash',
583           params  => [ $productId ],
584           id  => 1,
585       };
586     }
587     $res = $main::opsi_client->call($main::opsi_url, $callobj);
589     # JSON Query 2
590     $callobj = {
591       method  => 'getProductPropertyDefinitions_listOfHashes',
592       params  => [ $productId ],
593       id  => 1,
594     };
596     # Assemble options
597     my $res2 = $main::opsi_client->call($main::opsi_url, $callobj);
598     my $values = {};
599     my $descriptions = {};
600     if (not &check_opsi_res($res2)){
601         my $r= $res2->result;
603           foreach my $entr (@$r){
604             # Unroll values
605             my $cnv;
606             if (UNIVERSAL::isa( $entr->{'values'}, "ARRAY" )){
607               foreach my $v (@{$entr->{'values'}}){
608                 $cnv.= "<value>$v</value>";
609               }
610             } else {
611               $cnv= $entr->{'values'};
612             }
613             $values->{$entr->{'name'}}= $cnv;
614             $descriptions->{$entr->{'name'}}= "<description>".$entr->{'description'}."</description>";
615           }
616     }
618     if (not &check_opsi_res($res)){
619         my $r= $res->result;
620         foreach my $key (keys %{$r}) {
621             my $item= "\n<item>";
622             my $value= $r->{$key};
623             my $dsc= "";
624             my $vals= "";
625             if (defined $descriptions->{$key}){
626               $dsc= $descriptions->{$key};
627             }
628             if (defined $values->{$key}){
629               $vals= $values->{$key};
630             }
631             $item.= "<$key>$dsc<default>".xml_quote($value)."</default>$vals</$key>";
632             $item.= "</item>";
633             $xml_msg=~ s/<xxx><\/xxx>/$item<xxx><\/xxx>/;
634         }
635     }
637     $xml_msg=~ s/<xxx><\/xxx>//;
639     # Return message
640     return ( $xml_msg );
644 ## @method opsi_set_product_properties
645 # 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.
646 # @param msg - STRING - xml message with tags productId, action, state and optional hostId, action and state
647 # @param msg_hash - HASHREF - message information parsed into a hash
648 # @param session_id - INTEGER - POE session id of the processing of this message
649 # @return out_msg - STRING - feedback to GOsa in success and error case
650 sub opsi_set_product_properties {
651     my ($msg, $msg_hash, $session_id) = @_;
652     my $header = @{$msg_hash->{'header'}}[0];
653     my $source = @{$msg_hash->{'source'}}[0];
654     my $target = @{$msg_hash->{'target'}}[0];
655     my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
656     my ($productId, $hostId);
658     # Build return message with twisted target and source
659     my $out_hash = &main::create_xml_hash("answer_$header", $main::server_address, $source);
660     if (defined $forward_to_gosa) {
661         &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
662     }
664     # Sanity check of needed parameter
665     if ((not exists $msg_hash->{'productId'}) || (@{$msg_hash->{'productId'}} != 1) || (@{$msg_hash->{'productId'}}[0] eq ref 'HASH'))  {
666         &add_content2xml_hash($out_hash, "error_string", "no productId specified or productId tag invalid");
667         &add_content2xml_hash($out_hash, "error", "productId");
668         &main::daemon_log("$session_id ERROR: no productId specified or productId tag invalid: $msg", 1); 
669         return ( &create_xml_string($out_hash) );
670     }
671     if (not exists $msg_hash->{'item'}) {
672         &add_content2xml_hash($out_hash, "error_string", "message needs one xml-tag 'item' and within the xml-tags 'name' and 'value'");
673         &add_content2xml_hash($out_hash, "error", "item");
674         &main::daemon_log("$session_id ERROR: message needs one xml-tag 'item' and within the xml-tags 'name' and 'value': $msg", 1); 
675         return ( &create_xml_string($out_hash) );
676     } else {
677         if ((not exists @{$msg_hash->{'item'}}[0]->{'name'}) || (@{@{$msg_hash->{'item'}}[0]->{'name'}} != 1 )) {
678             &add_content2xml_hash($out_hash, "error_string", "message needs within the xml-tag 'item' one xml-tags 'name'");
679             &add_content2xml_hash($out_hash, "error", "name");
680             &main::daemon_log("$session_id ERROR: message needs within the xml-tag 'item' one xml-tags 'name': $msg", 1); 
681             return ( &create_xml_string($out_hash) );
682         }
683         if ((not exists @{$msg_hash->{'item'}}[0]->{'value'}) || (@{@{$msg_hash->{'item'}}[0]->{'value'}} != 1 )) {
684             &add_content2xml_hash($out_hash, "error_string", "message needs within the xml-tag 'item' one xml-tags 'value'");
685             &add_content2xml_hash($out_hash, "error", "value");
686             &main::daemon_log("$session_id ERROR: message needs within the xml-tag 'item' one xml-tags 'value': $msg", 1); 
687             return ( &create_xml_string($out_hash) );
688         }
689     }
690     # if no hostId is given, set_product_properties will act on globally
691     if ((exists $msg_hash->{'hostId'}) && (@{$msg_hash->{'hostId'}} > 1))  {
692         &add_content2xml_hash($out_hash, "error_string", "hostId contains no or more than one values");
693         &add_content2xml_hash($out_hash, "error", "hostId");
694         &main::daemon_log("$session_id ERROR: hostId contains no or more than one values: $msg", 1); 
695         return ( &create_xml_string($out_hash) );
696     }
698         
699     # Get productId
700     $productId =  @{$msg_hash->{'productId'}}[0];
701     &add_content2xml_hash($out_hash, "productId", $productId);
703     # Get hostId if defined
704     if (exists $msg_hash->{'hostId'}){
705         $hostId = @{$msg_hash->{'hostId'}}[0];
706         &add_content2xml_hash($out_hash, "hostId", $hostId);
707     }
709     # Set product states if requested
710     if (defined @{$msg_hash->{'action'}}[0]){
711         &_set_action($productId, @{$msg_hash->{'action'}}[0], $hostId);
712     }
713     if (defined @{$msg_hash->{'state'}}[0]){
714         &_set_state($productId, @{$msg_hash->{'state'}}[0], $hostId);
715     }
717     # Find properties
718     foreach my $item (@{$msg_hash->{'item'}}){
719         # JSON Query
720         my $callobj;
722         if (defined $hostId){
723             $callobj = {
724                 method  => 'setProductProperty',
725                 params  => [ $productId, $item->{'name'}[0], $item->{'value'}[0], $hostId ],
726                 id  => 1,
727             };
728         } else {
729             $callobj = {
730                 method  => 'setProductProperty',
731                 params  => [ $productId, $item->{'name'}[0], $item->{'value'}[0] ],
732                 id  => 1,
733             };
734         }
736         my $res = $main::opsi_client->call($main::opsi_url, $callobj);
737         my ($res_err, $res_err_string) = &check_opsi_res($res);
739         if ($res_err){
740             &main::daemon_log("$session_id ERROR: communication failed while setting '".$item->{'name'}[0]."': ".$res_err_string, 1);
741             &add_content2xml_hash($out_hash, "error", $res_err_string);
742         }
743     }
746     # Return message
747     return ( &create_xml_string($out_hash) );
751 ## @method opsi_get_client_hardware
752 # Reports client hardware inventory.
753 # @param msg - STRING - xml message with tag hostId
754 # @param msg_hash - HASHREF - message information parsed into a hash
755 # @param session_id - INTEGER - POE session id of the processing of this message
756 # @return out_msg - STRING - feedback to GOsa in success and error case
757 sub opsi_get_client_hardware {
758     my ($msg, $msg_hash, $session_id) = @_;
759     my $header = @{$msg_hash->{'header'}}[0];
760     my $source = @{$msg_hash->{'source'}}[0];
761     my $target = @{$msg_hash->{'target'}}[0];
762     my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
763     my $hostId;
764     my $error = 0;
765     my $xml_msg;
767     # Build return message with twisted target and source
768     my $out_hash = &main::create_xml_hash("answer_$header", $main::server_address, $source);
769     if (defined $forward_to_gosa) {
770       &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
771     }
773     # Sanity check of needed parameter
774     if ((exists $msg_hash->{'hostId'}) && (@{$msg_hash->{'hostId'}} != 1) || (@{$msg_hash->{'hostId'}}[0] eq ref 'HASH'))  {
775         $error++;
776         &add_content2xml_hash($out_hash, "error_string", "hostId contains no or more than one values");
777         &add_content2xml_hash($out_hash, "error", "hostId");
778         &main::daemon_log("$session_id ERROR: hostId contains no or more than one values: $msg", 1); 
779     }
781     if (not $error) {
783     # Get hostId
784         $hostId = @{$msg_hash->{'hostId'}}[0];
785         &add_content2xml_hash($out_hash, "hostId", "$hostId");
786         &add_content2xml_hash($out_hash, "xxx", "");
787     }    
789     # Move to XML string
790     $xml_msg= &create_xml_string($out_hash);
791     
792     if (not $error) {
794     # JSON Query
795         my $callobj = {
796             method  => 'getHardwareInformation_hash',
797             params  => [ $hostId ],
798             id  => 1,
799         };
801         my $res = $main::opsi_client->call($main::opsi_url, $callobj);
802         if (not &check_opsi_res($res)){
803             my $result= $res->result;
804             if (ref $result eq "HASH") {
805                 foreach my $r (keys %{$result}){
806                     my $item= "\n<item><id>".xml_quote($r)."</id>";
807                     my $value= $result->{$r};
808                     foreach my $sres (@{$value}){
810                         foreach my $dres (keys %{$sres}){
811                             if (defined $sres->{$dres}){
812                                 $item.= "<$dres>".xml_quote($sres->{$dres})."</$dres>";
813                             }
814                         }
816                     }
817                     $item.= "</item>";
818                     $xml_msg=~ s%<xxx></xxx>%$item<xxx></xxx>%;
820                 }
821             }
822         }
824         $xml_msg=~ s/<xxx><\/xxx>//;
826     }
828     # Return message
829     return ( $xml_msg );
833 ## @method opsi_list_clients
834 # Reports all Opsi clients. 
835 # @param msg - STRING - xml message 
836 # @param msg_hash - HASHREF - message information parsed into a hash
837 # @param session_id - INTEGER - POE session id of the processing of this message
838 # @return out_msg - STRING - feedback to GOsa in success and error case
839 sub opsi_list_clients {
840     my ($msg, $msg_hash, $session_id) = @_;
841     my $header = @{$msg_hash->{'header'}}[0];
842     my $source = @{$msg_hash->{'source'}}[0];
843     my $target = @{$msg_hash->{'target'}}[0];
844     my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
846     # Build return message with twisted target and source
847     my $out_hash = &main::create_xml_hash("answer_$header", $main::server_address, $source);
848     if (defined $forward_to_gosa) {
849       &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
850     }
851     &add_content2xml_hash($out_hash, "xxx", "");
853     # Move to XML string
854     my $xml_msg= &create_xml_string($out_hash);
856     # JSON Query
857     my $callobj = {
858         method  => 'getClients_listOfHashes',
859         params  => [ ],
860         id  => 1,
861     };
862     my $res = $main::opsi_client->call($main::opsi_url, $callobj);
863     if (not &check_opsi_res($res)){
864         foreach my $host (@{$res->result}){
865             my $item= "\n<item><name>".$host->{'hostId'}."</name>";
866             if (defined($host->{'description'})){
867                 $item.= "<description>".xml_quote($host->{'description'})."</description>";
868             }
869             if (defined($host->{'notes'})){
870                 $item.= "<notes>".xml_quote($host->{'notes'})."</notes>";
871             }
872             if (defined($host->{'lastSeen'})){
873                 $item.= "<lastSeen>".xml_quote($host->{'lastSeen'})."</lastSeen>";
874             }
876             $callobj = {
877               method  => 'getIpAddress',
878               params  => [ $host->{'hostId'} ],
879               id  => 1,
880             };
881             my $sres= $main::opsi_client->call($main::opsi_url, $callobj);
882             if ( not &check_opsi_res($sres)){
883               $item.= "<ip>".xml_quote($sres->result)."</ip>";
884             }
886             $callobj = {
887               method  => 'getMacAddress',
888               params  => [ $host->{'hostId'} ],
889               id  => 1,
890             };
891             $sres= $main::opsi_client->call($main::opsi_url, $callobj);
892             if ( not &check_opsi_res($sres)){
893                 $item.= "<mac>".xml_quote($sres->result)."</mac>";
894             }
895             $item.= "</item>";
896             $xml_msg=~ s%<xxx></xxx>%$item<xxx></xxx>%;
897         }
898     }
900     $xml_msg=~ s/<xxx><\/xxx>//;
901     return ( $xml_msg );
906 ## @method opsi_get_client_software
907 # Reports client software inventory.
908 # @param msg - STRING - xml message with tag hostId
909 # @param msg_hash - HASHREF - message information parsed into a hash
910 # @param session_id - INTEGER - POE session id of the processing of this message
911 # @return out_msg - STRING - feedback to GOsa in success and error case
912 sub opsi_get_client_software {
913     my ($msg, $msg_hash, $session_id) = @_;
914     my $header = @{$msg_hash->{'header'}}[0];
915     my $source = @{$msg_hash->{'source'}}[0];
916     my $target = @{$msg_hash->{'target'}}[0];
917     my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
918     my $error = 0;
919     my $hostId;
920     my $xml_msg;
922     # Build return message with twisted target and source
923     my $out_hash = &main::create_xml_hash("answer_$header", $main::server_address, $source);
924     if (defined $forward_to_gosa) {
925       &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
926     }
928     # Sanity check of needed parameter
929     if ((exists $msg_hash->{'hostId'}) && (@{$msg_hash->{'hostId'}} != 1) || (@{$msg_hash->{'hostId'}}[0] eq ref 'HASH'))  {
930         $error++;
931         &add_content2xml_hash($out_hash, "error_string", "hostId contains no or more than one values");
932         &add_content2xml_hash($out_hash, "error", "hostId");
933         &main::daemon_log("$session_id ERROR: hostId contains no or more than one values: $msg", 1); 
934     }
936     if (not $error) {
938     # Get hostId
939         $hostId = @{$msg_hash->{'hostId'}}[0];
940         &add_content2xml_hash($out_hash, "hostId", "$hostId");
941         &add_content2xml_hash($out_hash, "xxx", "");
942     }
944     $xml_msg= &create_xml_string($out_hash);
946     if (not $error) {
948     # JSON Query
949         my $callobj = {
950             method  => 'getSoftwareInformation_hash',
951             params  => [ $hostId ],
952             id  => 1,
953         };
955         my $res = $main::opsi_client->call($main::opsi_url, $callobj);
956         if (not &check_opsi_res($res)){
957             my $result= $res->result;
958 # TODO : Ist das hier schon fertig???   
959         }
961         $xml_msg=~ s/<xxx><\/xxx>//;
963     }
965     # Return message
966     return ( $xml_msg );
970 ## @method opsi_get_local_products
971 # Reports product for given hostId or globally.
972 # @param msg - STRING - xml message with optional tag hostId
973 # @param msg_hash - HASHREF - message information parsed into a hash
974 # @param session_id - INTEGER - POE session id of the processing of this message
975 # @return out_msg - STRING - feedback to GOsa in success and error case
976 sub opsi_get_local_products {
977     my ($msg, $msg_hash, $session_id) = @_;
978     my $header = @{$msg_hash->{'header'}}[0];
979     my $source = @{$msg_hash->{'source'}}[0];
980     my $target = @{$msg_hash->{'target'}}[0];
981     my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
982     my $hostId;
984     # Build return message with twisted target and source
985     my $out_hash = &main::create_xml_hash("answer_$header", $main::server_address, $source);
986     if (defined $forward_to_gosa) {
987         &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
988     }
989     &add_content2xml_hash($out_hash, "xxx", "");
991     # Get hostId if defined
992     if ((exists $msg_hash->{'hostId'}) && (@{$msg_hash->{'hostId'}} == 1))  {
993         $hostId = @{$msg_hash->{'hostId'}}[0];
994         &add_content2xml_hash($out_hash, "hostId", $hostId);
995     }
997     # Move to XML string
998     my $xml_msg= &create_xml_string($out_hash);
1000     # For hosts, only return the products that are or get installed
1001     my $callobj;
1002     $callobj = {
1003         method  => 'getLocalBootProductIds_list',
1004         params  => [ ],
1005         id  => 1,
1006     };
1008     my $res = $main::opsi_client->call($main::opsi_url, $callobj);
1009     my %r = ();
1010     for (@{$res->result}) { $r{$_} = 1 }
1012     if (not &check_opsi_res($res)){
1014         if (defined $hostId){
1015             $callobj = {
1016                 method  => 'getProductStates_hash',
1017                 params  => [ $hostId ],
1018                 id  => 1,
1019             };
1021             my $hres = $main::opsi_client->call($main::opsi_url, $callobj);
1022             if (not &check_opsi_res($hres)){
1023                 my $htmp= $hres->result->{$hostId};
1025                 # Check state != not_installed or action == setup -> load and add
1026                 foreach my $product (@{$htmp}){
1028                     if (!defined ($r{$product->{'productId'}})){
1029                         next;
1030                     }
1032                     # Now we've a couple of hashes...
1033                     if ($product->{'installationStatus'} ne "not_installed" or
1034                             $product->{'actionRequest'} eq "setup"){
1035                         my $state= "<state>".$product->{'installationStatus'}."</state><action>".$product->{'actionRequest'}."</action>";
1037                         $callobj = {
1038                             method  => 'getProduct_hash',
1039                             params  => [ $product->{'productId'} ],
1040                             id  => 1,
1041                         };
1043                         my $sres = $main::opsi_client->call($main::opsi_url, $callobj);
1044                         if (not &check_opsi_res($sres)){
1045                             my $tres= $sres->result;
1047                             my $name= xml_quote($tres->{'name'});
1048                             my $r= $product->{'productId'};
1049                             my $description= xml_quote($tres->{'description'});
1050                             $name=~ s/\//\\\//;
1051                             $description=~ s/\//\\\//;
1052                             $xml_msg=~ s/<xxx><\/xxx>/\n<item><productId>$r<\/productId><name><\/name><description>$description<\/description><\/item>$state<xxx><\/xxx>/;
1053                         }
1055                     }
1056                 }
1058             }
1060         } else {
1061             foreach my $r (@{$res->result}) {
1062                 $callobj = {
1063                     method  => 'getProduct_hash',
1064                     params  => [ $r ],
1065                     id  => 1,
1066                 };
1068                 my $sres = $main::opsi_client->call($main::opsi_url, $callobj);
1069                 if (not &check_opsi_res($sres)){
1070                     my $tres= $sres->result;
1072                     my $name= xml_quote($tres->{'name'});
1073                     my $description= xml_quote($tres->{'description'});
1074                     $name=~ s/\//\\\//;
1075                     $description=~ s/\//\\\//;
1076                     $xml_msg=~ s/<xxx><\/xxx>/\n<item><productId>$r<\/productId><name><\/name><description>$description<\/description><\/item><xxx><\/xxx>/;
1077                 }
1079             }
1081         }
1082     }
1084     $xml_msg=~ s/<xxx><\/xxx>//;
1086     # Retrun Message
1087     return ( $xml_msg );
1091 ## @method opsi_del_client
1092 # Deletes a client from Opsi.
1093 # @param msg - STRING - xml message with tag hostId
1094 # @param msg_hash - HASHREF - message information parsed into a hash
1095 # @param session_id - INTEGER - POE session id of the processing of this message
1096 # @return out_msg - STRING - feedback to GOsa in success and error case
1097 sub opsi_del_client {
1098     my ($msg, $msg_hash, $session_id) = @_;
1099     my $header = @{$msg_hash->{'header'}}[0];
1100     my $source = @{$msg_hash->{'source'}}[0];
1101     my $target = @{$msg_hash->{'target'}}[0];
1102     my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
1103     my $hostId;
1104     my $error = 0;
1106     # Build return message with twisted target and source
1107     my $out_hash = &main::create_xml_hash("answer_$header", $main::server_address, $source);
1108     if (defined $forward_to_gosa) {
1109       &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
1110     }
1112     # Sanity check of needed parameter
1113     if ((exists $msg_hash->{'hostId'}) && (@{$msg_hash->{'hostId'}} != 1) || (@{$msg_hash->{'hostId'}}[0] eq ref 'HASH'))  {
1114         $error++;
1115         &add_content2xml_hash($out_hash, "error_string", "hostId contains no or more than one values");
1116         &add_content2xml_hash($out_hash, "error", "hostId");
1117         &main::daemon_log("$session_id ERROR: hostId contains no or more than one values: $msg", 1); 
1118     }
1120     if (not $error) {
1122     # Get hostId
1123         $hostId = @{$msg_hash->{'hostId'}}[0];
1124         &add_content2xml_hash($out_hash, "hostId", "$hostId");
1126     # JSON Query
1127         my $callobj = {
1128             method  => 'deleteClient',
1129             params  => [ $hostId ],
1130             id  => 1,
1131         };
1132         my $res = $main::opsi_client->call($main::opsi_url, $callobj);
1133     }
1135     # Move to XML string
1136     my $xml_msg= &create_xml_string($out_hash);
1138     # Return message
1139     return ( $xml_msg );
1143 ## @method opsi_install_client
1144 # Set a client in Opsi to install and trigger a wake on lan message (WOL).  
1145 # @param msg - STRING - xml message with tags hostId, macaddress
1146 # @param msg_hash - HASHREF - message information parsed into a hash
1147 # @param session_id - INTEGER - POE session id of the processing of this message
1148 # @return out_msg - STRING - feedback to GOsa in success and error case
1149 sub opsi_install_client {
1150     my ($msg, $msg_hash, $session_id) = @_;
1151     my $header = @{$msg_hash->{'header'}}[0];
1152     my $source = @{$msg_hash->{'source'}}[0];
1153     my $target = @{$msg_hash->{'target'}}[0];
1154     my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
1157     my ($hostId, $macaddress);
1159     my $error = 0;
1160     my @out_msg_l;
1162     # Build return message with twisted target and source
1163     my $out_hash = &main::create_xml_hash("answer_$header", $main::server_address, $source);
1164     if (defined $forward_to_gosa) {
1165         &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
1166     }
1168     # Sanity check of needed parameter
1169     if ((not exists $msg_hash->{'hostId'}) || (@{$msg_hash->{'hostId'}} != 1) || (@{$msg_hash->{'hostId'}}[0] eq ref 'HASH'))  {
1170         $error++;
1171         &add_content2xml_hash($out_hash, "error_string", "no hostId specified or hostId tag invalid");
1172         &add_content2xml_hash($out_hash, "error", "hostId");
1173         &main::daemon_log("$session_id ERROR: no hostId specified or hostId tag invalid: $msg", 1); 
1174     }
1175     if ((not exists $msg_hash->{'macaddress'}) || (@{$msg_hash->{'macaddress'}} != 1) || (@{$msg_hash->{'macaddress'}}[0] eq ref 'HASH') )  {
1176         $error++;
1177         &add_content2xml_hash($out_hash, "error_string", "no macaddress specified or macaddress tag invalid");
1178         &add_content2xml_hash($out_hash, "error", "macaddress");
1179         &main::daemon_log("$session_id ERROR: no macaddress specified or macaddress tag invalid: $msg", 1); 
1180     } else {
1181         if ((exists $msg_hash->{'macaddress'}) && 
1182                 ($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)) {  
1183             $macaddress = $1; 
1184         } else { 
1185             $error ++; 
1186             &add_content2xml_hash($out_hash, "error_string", "given mac address is not correct");
1187             &add_content2xml_hash($out_hash, "error", "macaddress");
1188             &main::daemon_log("$session_id ERROR: given mac address is not correct: $msg", 1); 
1189         }
1190     }
1192     if (not $error) {
1194     # Get hostId
1195         $hostId = @{$msg_hash->{'hostId'}}[0];
1196         &add_content2xml_hash($out_hash, "hostId", "$hostId");
1198         # Load all products for this host with status != "not_installed" or actionRequest != "none"
1199         my $callobj = {
1200             method  => 'getProductStates_hash',
1201             params  => [ $hostId ],
1202             id  => 1,
1203         };
1205         my $hres = $main::opsi_client->call($main::opsi_url, $callobj);
1206         if (not &check_opsi_res($hres)){
1207             my $htmp= $hres->result->{$hostId};
1209             # check state != not_installed or action == setup -> load and add
1210             foreach my $product (@{$htmp}){
1211                 # Now we've a couple of hashes...
1212                 if ($product->{'installationStatus'} ne "not_installed" or
1213                         $product->{'actionRequest'} ne "none"){
1215                     # Do an action request for all these -> "setup".
1216                     $callobj = {
1217                         method  => 'setProductActionRequest',
1218                         params  => [ $product->{'productId'}, $hostId, "setup" ],
1219                         id  => 1,
1220                     };
1221                     my $res = $main::opsi_client->call($main::opsi_url, $callobj);
1222                     my ($res_err, $res_err_string) = &check_opsi_res($res);
1223                     if ($res_err){
1224                         &main::daemon_log("$session_id ERROR: cannot set product action request for '$hostId': ".$product->{'productId'}, 1);
1225                     } else {
1226                         &main::daemon_log("$session_id INFO: requesting 'setup' for '".$product->{'productId'}."' on $hostId", 1);
1227                     }
1228                 }
1229             }
1230         }
1231         push(@out_msg_l, &create_xml_string($out_hash));
1232     
1234     # Build wakeup message for client
1235         if (not $error) {
1236             my $wakeup_hash = &create_xml_hash("trigger_wake", "GOSA", "KNOWN_SERVER");
1237             &add_content2xml_hash($wakeup_hash, 'macAddress', $macaddress);
1238             my $wakeup_msg = &create_xml_string($wakeup_hash);
1239             push(@out_msg_l, $wakeup_msg);
1241             # invoke trigger wake for this gosa-si-server
1242             &main::server_server_com::trigger_wake($wakeup_msg, $wakeup_hash, $session_id);
1243         }
1244     }
1245     
1246     # Return messages
1247     return @out_msg_l;
1251 ## @method _set_action
1252 # Set action for an Opsi client
1253 # @param product - STRING - Opsi product
1254 # @param action - STRING - action
1255 # @param hostId - STRING - Opsi hostId
1256 sub _set_action {
1257   my $product= shift;
1258   my $action = shift;
1259   my $hostId = shift;
1260   my $callobj;
1262   $callobj = {
1263     method  => 'setProductActionRequest',
1264     params  => [ $product, $hostId, $action],
1265     id  => 1,
1266   };
1268   $main::opsi_client->call($main::opsi_url, $callobj);
1271 ## @method _set_state
1272 # Set state for an Opsi client
1273 # @param product - STRING - Opsi product
1274 # @param action - STRING - state
1275 # @param hostId - STRING - Opsi hostId
1276 sub _set_state {
1277   my $product = shift;
1278   my $state = shift;
1279   my $hostId = shift;
1280   my $callobj;
1282   $callobj = {
1283     method  => 'setProductState',
1284     params  => [ $product, $hostId, $state ],
1285     id  => 1,
1286   };
1288   $main::opsi_client->call($main::opsi_url, $callobj);
1291 1;