This file is indexed.

/usr/share/gosa/plugins/admin/opsi/class_opsi.inc is in gosa-plugin-opsi 2.7.4+reloaded2-13+deb9u1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
<?php
/*
   This code is part of GOsa (https://gosa.gonicus.de)
   Copyright (C) 2008  Fabian Hickert

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


/********

  public function __construct($config)
  public function enabled()
  function get_hosts_for_system_management()
  private function xml_to_array($xml,$alternative_method = FALSE)
  public function send_action($type,$hostId,$mac)
  public function list_clients( $hostId = "")
  public function add_client($hostId,$macaddress,$notes,$description)
  public function modify_client($hostId,$mac,$notes,$description)
  public function get_netboot_products($host = "")
  public function get_local_products($host = "")
  public function get_product_properties($productId,$hostId = "")
  public function set_product_properties($productId,$cfg,$hostId = "")
  public function add_product_to_client($productId,$hostId)
  public function del_product_from_client($productId,$hostId)
  public function get_client_hardware($hostId)
  public function get_client_software($hostId)
  public function del_client($hostId)
  public function job_opsi_install_client($hostId,$mac)

 ********/


/*! \brief  This is the opsi base class, it handles 
  .          gosa daemon requests and prepares data for opsi plugins.
 */
class opsi extends gosaSupportDaemon 
{
    private $config = NULL;
    protected $use_alternative_xml_parse_method = TRUE;
    protected $target = "";

    /*! \brief 		Create opsi object.
      @param
      @return 		
     */
    public function __construct($config)
    {
        $this->config = $config;
        gosaSupportDaemon::__construct($config);

        /* Detect the target opsi host 
         */
        if($this->config->pluginEnabled("faiManagement")){
            $opsi_hosts = $this->get_hosts_with_module("opsi_com");

            /* Just use the first result of the opsi hosts 
             */
            if(count($opsi_hosts) == 1 && isset($opsi_hosts[0])){
                $this->target = $opsi_hosts[0];
            }elseif(count($opsi_hosts) > 1){
                $this->target = $opsi_hosts[0];
                msg_dialog::display(_("Opsi"),sprintf(_("More than one Opsi server were found, using the first result '%s'."),$this->target));
            }
        }
    }


    public function enabled()
    {
        return($this->config->pluginEnabled('faiManagement') && !empty($this->target));
    }


    /******************
      Opsi handling 
     ******************/

    function get_hosts_for_system_management()
    {
        $res = $this->list_clients();
        $data = array();
        $ui = get_userinfo();
        foreach($res as $entry){
            if(!isset($entry['MAC'][0]['VALUE'])) $entry['MAC'][0]['VALUE'] = "";
            $obj = array(
                    "dn"          => "opsi:=".$entry['NAME'][0]['VALUE'].",".get_ou("wingeneric", "sambaMachineAccountRDN").$this->config->current['BASE'],
                    "objectClass" => array("gosa_opsi_client"),
                    "cn"          => array(0 => $entry['NAME'][0]['VALUE']),
                    "macAddress"  => array(0 => $entry['MAC'][0]['VALUE']),
                    "opsi_notes"  => array(0 => $entry['NOTES'][0]['VALUE']));

            /* Check permissions */
            $opsi_acl = $ui->get_permissions($obj['dn'],"opsi/opsiGeneric");
            if(preg_match("/r/",$opsi_acl)){
                if(!empty($entry['DESCRIPTION'][0]['VALUE'])){ 
                    $obj["description"]= array();
                    $obj["description"][0]= $entry['DESCRIPTION'][0]['VALUE'];
                }
                $data[] = $obj;
            }
        }

        return($data);
    }


    /*! \brief  Maps all xml to array conversion to an alternative method
      then used in the parent class 'gosaSupportDaemon'.
      The alternative method is able to handle more complex data.
     */
    private function xml_to_array($xml,$alternative_method = FALSE)
    {
        return(gosaSupportDaemon::xml_to_array($xml,TRUE));
    }


    /*! \brief  Trigger an event like wake or install for a specific hostId. 
     */
    public function send_action($type,$hostId,$mac)
    {
        switch($type){
            case 'install'  :  $this->job_opsi_install_client($hostId,$mac); break;
            default         :  trigger_error('Unknown type '.$type.'.');
        }
    }


    public function job_opsi_activate_client($id,$mac)
    {
        $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
        $o_queue = new gosaSupportDaemon();
        if(isset($events['TRIGGERED']['DaemonEvent_activate'])){
            $evt = $events['TRIGGERED']['DaemonEvent_activate'];
            $tmp = new $evt['CLASS_NAME']($this->config);
            $tmp->set_type(TRIGGERED_EVENT);
            $tmp->add_targets(array($mac));
            if(!$o_queue->append($tmp)){
                msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
            }
        }
    }



    /******************
      SI Communication functions
     ******************/



    /*! \brief 		Returns a list of all opsi clients.
      @param
      @return 		
     */
    public function list_clients( $hostId = "")
    {
        $data   = array();
        $res    = $this->send_data("gosa_opsi_list_clients",$this->target,$data,TRUE);
        $items  = array();
        if(isset($res['XML'][0]['ITEM'])){
            $items = $res['XML'][0]['ITEM'];
        }
        return($items);
    }


    /*! \brief 		Adds a new opsi client.
      @param
      @return 		
     */
    public function add_client($hostId,$macaddress,$notes,$description)
    {
        $data = array("hostId" => $hostId,"macaddress" => $macaddress);

        if(empty($hostId)){
            trigger_error("No valid host id given, check parameter 1.");
            return;
        }

        /* Add optional attributes */ 
        foreach(array("notes","description") as $attr) {
            if(!empty($$attr)){
                $data[$attr] = $$attr;
            }
        }

        /* Query SI server */
        $res    = $this->send_data("gosa_opsi_add_client",$this->target,$data,TRUE);
    }


    /*! \brief 		Modify an opsi client.
      @param
      @return 		
     */
    public function modify_client($hostId,$mac,$notes,$description)
    {
        $data = array("hostId" => $hostId,"mac" => $mac);

        if(empty($hostId)){
            trigger_error("No valid host id given, check parameter 1.");
            return;
        }

        /* Add optional attributes */ 
        foreach(array("notes","description") as $attr) {
            $data[$attr] = $$attr;
        }

        /* Query SI server */
        $res = $this->send_data("gosa_opsi_modify_client",$this->target,$data,TRUE);
    }



    /*! \brief 		Returns a list of netboot products.
      @param
      @return 		
     */
    public function get_netboot_products($host = "")
    {
        /* Append host attribute to query data 
         */
        $data = array();
        if(!empty($host)){
            $data['hostId'] = trim($host);
        }

        $res    = $this->send_data("gosa_opsi_get_netboot_products",$this->target,$data,TRUE);
        $items = array();
        if(isset($res['XML'][0]['ITEM'])){
            foreach($res['XML'][0]['ITEM'] as $entry){
                $e = array("DESC" => $entry['DESCRIPTION'][0]['VALUE'],
                        "NAME" => $entry['PRODUCTID'][0]['VALUE']);
                $items[$entry['PRODUCTID'][0]['VALUE']] = $e;
            }
        }
        return($items);
    }


    /*! \brief 		Returns a list of all local products.
      @param
      @return 		
     */
    public function get_local_products($host = "")
    {
        /* Append host attribute to query data 
         */
        $data = array();
        if(!empty($host)){
            $data['hostId'] = trim($host);
        }

        $res    = $this->send_data("gosa_opsi_get_local_products",$this->target,$data,TRUE);
        $items = array();
        if(isset($res['XML'][0]['ITEM'])){
            foreach($res['XML'][0]['ITEM'] as $entry){
                $e = array("DESC" => $entry['DESCRIPTION'][0]['VALUE'],
                        "NAME" => $entry['PRODUCTID'][0]['VALUE']);
                $items[$entry['PRODUCTID'][0]['VALUE']] = $e; 
            }
        }
        return($items);
    }


    /*! \brief 		Returns a list of netboot products.
      @param
      @return 		
     */
    public function get_full_product_host_information($host = "")
    {
        /* Append host attribute to query data 
         */
        $data = array();
        if(!empty($host)){
            $data['hostId'] = trim($host);
        }

        $res    = $this->send_data("gosa_opsi_get_full_product_host_information",$this->target,$data,TRUE);
        $items = array();
        $attrs = array("TYPE","PRIORITY","ONCESCRIPT","LICENSEREQUIRED","PACKAGEVERSION","PRODUCTVERSION",
                "ADVICE","SETUPSCRIPT","WINDOWSSOFTWAREIDS","PXECONFIGTEMPLATE","NAME","CREATIONTIMESTAMP", "TYPE",
                "ALWAYSSCRIPT","PRODUCTID","DESCRIPTION","UNINSTALLSCRIPT","UPDATESCRIPT","PRODUCTCLASSNAMES");

        if(isset($res['XML'][0]['ITEM'])){
            foreach($res['XML'][0]['ITEM'] as $entry){
                $e = array();
                foreach($attrs as $attr){
                    if(isset($entry[$attr])){
                        foreach($entry[$attr] as $key => $value){
                            if(isset($value['VALUE'])){
                                $e['data'][$attr] = $value['VALUE'];
                            }elseif($value['ELEMENT']){
                                foreach($value['ELEMENT'] as $element){
                                    $e['data'][$attr][] = $element['VALUE'];
                                }
                            }
                        }
                    }
                }

                $e["configurable"] = FALSE;
                if(isset($entry['PROPERTIES']) && count($entry['PROPERTIES'])){
                    $e["configurable"] = TRUE;
                    $p_data = array();

                    foreach($entry['PROPERTIES'][0] as $p_name => $p_values){
                        if(empty($p_values)) continue;
                        $p_data[$p_name]= array();
                        foreach(array('CURRENT','DEFAULT','DESCRIPTION') as $p_tmp){
                            if(isset($p_values[0][$p_tmp])){
                                if(isset($p_values[0][$p_tmp][0]['VALUE'])){
                                    $p_data[$p_name][$p_tmp] = $p_values[0][$p_tmp][0]['VALUE'];
                                }
                            }
                        }

                        if(isset($p_values[0]['VALUES'][0]['ELEMENT'])){
                            foreach($p_values[0]['VALUES'][0]['ELEMENT'] as $val){
                                $p_data[$p_name]['VALUE'][] = $val['VALUE'];
                            }
                            $p_data[$p_name]['VALUE_CNT'] = count($p_data[$p_name]['VALUE']);
                        } 
                    }

                    $e["data"]['PROPERTIES'] = $p_data;
                }

		$e["request"] = "";
                if(isset($entry['ACTIONREQUEST'])){
                    $e["request"] = $entry['ACTIONREQUEST']['0']['VALUE'];
                }

		$e["installed"] = FALSE;
                if(($e['request'] == "setup") || (isset($entry['INSTALLATIONSTATUS']) && $entry['INSTALLATIONSTATUS']['0']['VALUE'] == 'installed')){
                    $e["installed"] = TRUE;
                }


                $e["requires_licence"] = FALSE;
                if(isset($e['data']['LICENSEREQUIRED']) && preg_match("/true/i",$e['data']['LICENSEREQUIRED'])){
                    $e["requires_licence"] = TRUE;
                }
                $items[$entry['PRODUCTID'][0]['VALUE']] = $e;
            }
        }
        return($items);
    }


    /*! \brief 		Returns a list of all product properties. \ 
      .           Additionally you can specify the host parameter to \
      .           get host specific product properties
      @param
      @return 		
     */
    public function get_product_properties($productId,$hostId = "")
    {
        $data = array("productId" => $productId);

        /* Append host attribute to query data 
         */
        if(!empty($hostId)){
            $data['hostId'] = trim($hostId);
        }

        /* Check parameter */ 
        if(empty($productId)){
            trigger_error("No valid product id given, check parameter 1.");
            return(array());
        }

        /* Query SI server */
        $res    = $this->send_data("gosa_opsi_get_product_properties",$this->target,$data,TRUE);
        $items  = array();
        if(isset($res['XML'][0]['ITEM'])){   
            foreach($res['XML'][0]['ITEM'] as $entry){
                foreach($entry as $name => $val){

                    foreach(array("DESCRIPTION","CURRENT") as $attr){
                        $items[$name][$attr] = "";
                        if(isset($val[0][$attr])){
                            $items[$name][$attr] = $val[0][$attr][0]['VALUE'];
                        }
                    }
                    $items[$name]['VALUE'] = array();
                    if(isset($val['0']['VALUE'])){
                        foreach($val['0']['VALUE'] as $value){
                            $items[$name]['VALUE'][] = $value['VALUE'];
                        }
                    }
                    $items[$name]['VALUE_CNT'] = count($items[$name]['VALUE']);
                }
            }
        }
        return($items);
    }


    /*! \brief 		Set product properties, globally or per host. 
      @param
      @return 		
     */
    public function set_product_properties($productId,$cfg,$hostId = "")
    {
        $data = array("productId" => $productId);

        /* Append host attribute to query data 
         */
        if(!empty($hostId)){
            $data['hostId'] = trim($hostId);
        }

        /* Check parameter */ 
        if(empty($productId)){
            trigger_error("No valid product id given, check parameter 1.");
            return(array());
        }

        if(!count($cfg)) return;

        /* Add properties */
        $data['item'] = array();
        foreach($cfg as $name => $value){
            $data['item'][] = "<name>".$name."</name><value>".$value['CURRENT']."</value>";
        }  

        /* Query SI server */
        $res    = $this->send_data("gosa_opsi_set_product_properties",$this->target,$data,TRUE);
    }


    /*! \brief 		Adds a given product to a client.
      @param
      @return 		
     */
    public function add_product_to_client($productId,$hostId)
    {
        $data = array("productId" => $productId,"hostId" => $hostId);

        /* Check parameter */ 
        if(empty($productId)){
            trigger_error("No valid product id given, check parameter 1.");
            return;
        }
        if(empty($hostId)){
            trigger_error("No valid host id given, check parameter 2.");
            return;
        }

        /* Query SI server */
        $res    = $this->send_data("gosa_opsi_add_product_to_client",$this->target,$data,TRUE);
    }


    /*! \brief      Removes a given product from a client.
      @param
      @return
     */
    public function del_product_from_client($productId,$hostId)
    {
        $data = array("productId" => $productId,"hostId" => $hostId);

        /* Check parameter */ 
        if(empty($productId)){
            trigger_error("No valid product id given, check parameter 1.");
            return;
        }
        if(empty($hostId)){
            trigger_error("No valid host id given, check parameter 2.");
            return;
        }

        /* Query SI server */
        $res    = $this->send_data("gosa_opsi_del_product_from_client",$this->target,$data,TRUE);
    }


    /*! \brief 		Returns the clients hardware setup.
      @param
      @return 		
     */
    public function get_client_hardware($hostId)
    {
        $data = array("hostId" => $hostId);

        /* Check parameter */ 
        if(empty($hostId)){
            trigger_error("No valid host id given, check parameter 1.");
            return;
        }

        /* Query SI server */
        $res    = $this->send_data("gosa_opsi_get_client_hardware",$this->target,$data,TRUE);
        if(isset($res['XML'][0]['ITEM'])){
            return($res['XML'][0]['ITEM']);
        }
        return(array());
    }


    /*! \brief 		Returns the clients software setup.
      @param
      @return 		
     */
    public function get_client_software($hostId)
    {
        $data = array("hostId" => $hostId);

        /* Check parameter */ 
        if(empty($hostId)){
            trigger_error("No valid host id given, check parameter 1.");
            return;
        }

        /* Query SI server */
        $res    = $this->send_data("gosa_opsi_get_client_software",$this->target,$data,TRUE);
        if(isset($res['XML'][0]['ITEM'])){
            return($res['XML'][0]['ITEM']);
        }
        return(array());
    }



    /*! \brief 		Deletes the given opsi client.
      @param
      @return 		
     */
    public function del_client($hostId)
    {
        $data = array("hostId" => $hostId);

        /* Check parameter */ 
        if(empty($hostId)){
            trigger_error("No valid host id given, check parameter 1.");
            return;
        }

        /* Query SI server */
        $res    = $this->send_data("gosa_opsi_del_client",$this->target,$data,TRUE);
        if(isset($res['XML'][0]['ITEM'])){
            return($res['XML'][0]['ITEM']);
        }
        return(array());
    }


    /*! \brief 		Triggers install/reinstall of an opsi client.
      @param
      @return 		
     */
    public function job_opsi_install_client($hostId,$mac)
    {
        $data = array("hostId" => $hostId,"macaddress"=>$mac);

        /* Check parameter */ 
        if(empty($hostId)){
            trigger_error("No valid host id given, check parameter 1.");
            return;
        }

        /* Query SI server */
        $this->send_data("job_opsi_install_client",$this->target,$data,TRUE);
    }
}
// vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
?>