This repository has been archived by the owner on Dec 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Openfire.php
752 lines (597 loc) · 22.4 KB
/
Openfire.php
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
<?php
/**
* Openfire server class.
*
* @category apps
* @package openfire
* @subpackage controllers
* @author Marc Laporte
* @copyright 2016 Marc Laporte
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License version 3 or later
* @link https://github.com/eglooca/app-openfire
*/
///////////////////////////////////////////////////////////////////////////////
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// N A M E S P A C E
///////////////////////////////////////////////////////////////////////////////
namespace clearos\apps\openfire;
///////////////////////////////////////////////////////////////////////////////
// B O O T S T R A P
///////////////////////////////////////////////////////////////////////////////
$bootstrap = getenv('CLEAROS_BOOTSTRAP') ? getenv('CLEAROS_BOOTSTRAP') : '/usr/clearos/framework/shared';
require_once $bootstrap . '/bootstrap.php';
///////////////////////////////////////////////////////////////////////////////
// T R A N S L A T I O N S
///////////////////////////////////////////////////////////////////////////////
clearos_load_language('openfire');
clearos_load_language('network');
clearos_load_language('certificate_manager');
///////////////////////////////////////////////////////////////////////////////
// D E P E N D E N C I E S
///////////////////////////////////////////////////////////////////////////////
// Classes
//--------
use \clearos\apps\base\Daemon as Daemon;
use \clearos\apps\base\File as File;
use \clearos\apps\base\Shell as Shell;
use \clearos\apps\certificate_manager\Certificate_Manager as Certificate_Manager;
use \clearos\apps\groups\Group_Factory as Group_Factory;
use \clearos\apps\ldap\LDAP_Factory as LDAP_Factory;
use \clearos\apps\network\Domain as Domain;
use \clearos\apps\network\Hostname as Hostname;
use \clearos\apps\network\Network_Utils as Network_Utils;
use \clearos\apps\system_database\System_Database as System_Database;
use \clearos\apps\users\User_Factory as User_Factory;
use \clearos\apps\users\User_Utilities as User_Utilities;
clearos_load_library('base/Daemon');
clearos_load_library('base/File');
clearos_load_library('base/Shell');
clearos_load_library('certificate_manager/Certificate_Manager');
clearos_load_library('groups/Group_Factory');
clearos_load_library('ldap/LDAP_Factory');
clearos_load_library('network/Domain');
clearos_load_library('network/Hostname');
clearos_load_library('network/Network_Utils');
clearos_load_library('system_database/System_Database');
clearos_load_library('users/User_Factory');
clearos_load_library('users/User_Utilities');
// Exceptions
//-----------
use \clearos\apps\base\Validation_Exception as Validation_Exception;
clearos_load_library('base/Validation_Exception');
///////////////////////////////////////////////////////////////////////////////
// C L A S S
///////////////////////////////////////////////////////////////////////////////
/**
* Openfire server class.
*
* @category apps
* @package openfire
* @subpackage controllers
* @author Marc Laporte
* @copyright 2016 Marc Laporte
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License version 3 or later
* @link https://github.com/eglooca/app-openfire
*/
class Openfire extends Daemon
{
///////////////////////////////////////////////////////////////////////////////
// C O N S T A N T S
///////////////////////////////////////////////////////////////////////////////
const DATABASE = 'openfire';
const APP_BASENAME = 'openfire';
const FILE_CONFIG = '/usr/share/openfire/conf/openfire.xml';
const FILE_SECURITY_CONFIG = '/usr/share/openfire/conf/security.xml';
const FILE_PKCS12 = '/var/clearos/openfire/keystore.p12';
const FILE_KEYSTORE = '/usr/share/openfire/resources/security/keystore';
const CONSTANT_KEYSTORE_PW = 'changeit';
const PATH_HOTDEPLOY = '/usr/share/openfire/resources/security/hotdeploy';
const PROPERTY_ADMINS = 'admin.authorizedJIDs';
const PROPERTY_XMPP_DOMAIN = 'xmpp.domain';
const PROPERTY_XMPP_FQDN = 'xmpp.fqdn';
const DEFAULT_OFMEET_USER = 'focus';
const OPENFIRE_USER = 'openfire';
const OPENFIRE_GROUP = 'openfire';
const COMMAND_KEYTOOL = '/usr/bin/keytool';
const COMMAND_OPENSSL = '/usr/bin/openssl';
///////////////////////////////////////////////////////////////////////////////
// M E T H O D S
///////////////////////////////////////////////////////////////////////////////
/**
* Openfire constructor.
*/
public function __construct()
{
clearos_profile(__METHOD__, __LINE__);
parent::__construct('openfire');
}
/**
* Returns the Openfire administrator account.
*
* @return string Openfire administrator account
* @throws Engine_Exception
*/
public function get_admin()
{
clearos_profile(__METHOD__, __LINE__);
$list = $this->_get_property(self::PROPERTY_ADMINS);
$first_match = preg_replace('/,.*/', '', $list);
$username_only = preg_replace('/@.*/', '', $first_match);
return $username_only;
}
/**
* Returns configured digital certificate.
*
* @return string basename of digital certificate
* @throws Engine_Exception
*/
public function get_digital_certificate()
{
clearos_profile(__METHOD__, __LINE__);
$certificate_manager = new Certificate_Manager();
$registered = $certificate_manager->get_registered_certificate(self::APP_BASENAME, '-');
return $registered;
}
/**
* Returns list of available certificates.
*
* @return array list of available certificates
* @throws Engine_Exception
*/
public function get_digital_certificates()
{
clearos_profile(__METHOD__, __LINE__);
$certificate_manager = new Certificate_Manager();
$hostnames = $certificate_manager->get_secure_hostnames();
return $hostnames;
}
/**
* Returns XMPP domain.
*
* @param boolean $return_default return a default value if XMPP domain is not set
*
* @return string XMPP domain
* @throws Engine_Exception
*/
public function get_xmpp_domain($return_default = TRUE)
{
clearos_profile(__METHOD__, __LINE__);
$domain = $this->_get_property(self::PROPERTY_XMPP_DOMAIN);
if ($return_default && empty($domain)) {
$domainObject = new Domain();
$domain = $domainObject->get_default();
}
return $domain;
}
/**
* Returns XMPP fully-qualified domain name.
*
* @param boolean $return_default return a default value if XMPP FQDN is not set
* @return string XMPP domain
* @throws Engine_Exception
*/
public function get_xmpp_fqdn($return_default = TRUE)
{
clearos_profile(__METHOD__, __LINE__);
$domain = $this->_get_property(self::PROPERTY_XMPP_FQDN);
if ($return_default && empty($domain)) {
$hostname = new Hostname();
$domain = $hostname->get_internet_hostname();
}
return $domain;
}
/**
* Returns list of possible Openfire administrators.
*
* @return array list of possible Openfire administrators
* @throws Engine_Exception
*/
public function get_possible_admins()
{
clearos_profile(__METHOD__, __LINE__);
$group = Group_Factory::create('openfire_plugin');
$raw_list = $group->get_members();
$list = [];
foreach ($raw_list as $member) {
if ($member != self::DEFAULT_OFMEET_USER)
$list[] = $member;
}
return $list;
}
/**
* Returns initialization state.
*
* @return boolean TRUE if Openfire has been initialized
* @throws Engine_Exception
*/
public function is_initialized()
{
clearos_profile(__METHOD__, __LINE__);
$xmpp_domain = $this->get_xmpp_domain(FALSE);
if (empty($xmpp_domain))
return FALSE;
else
return TRUE;
}
/**
* Sets the Openfire administrator account.
*
* @return void
* @throws Engine_Exception
*/
public function set_admin($username)
{
clearos_profile(__METHOD__, __LINE__);
// Validation_Exception::is_valid($this->validate_username($username));
$domain = $this->get_xmpp_domain();
$username = $username . '@' . $domain;
$this->_set_property(self::PROPERTY_ADMINS, $username);
}
/**
* Sets the digital certificate.
*
* @return void
* @throws Engine_Exception
*/
public function set_certificate($certificate)
{
clearos_profile(__METHOD__, __LINE__);
Validation_Exception::is_valid($this->validate_certificate($certificate));
// Register certificate with Certificate Manager
//----------------------------------------------
$cert_object['-'] = $certificate;
$certificate_manager = new Certificate_Manager();
$certificate_manager->register($cert_object, 'openfire', lang('openfire_app_name'));
// Import via keytool
//-------------------
$details = $certificate_manager->get_certificate($certificate);
if (array_key_exists('fullchain-filename', $details))
$cert_filename = $details['fullchain-filename'];
else
$cert_filename = $details['certificate-filename'];
$shell = new Shell();
$shell->execute(
self::COMMAND_OPENSSL,
'pkcs12 -export ' .
'-name ' . $certificate . ' ' .
'-out ' . self::FILE_PKCS12 . ' -inkey ' . $details['key-filename'] . ' -in ' . $cert_filename . ' ' .
'-password "pass:' . self::CONSTANT_KEYSTORE_PW . '"',
TRUE
);
try {
$options['validate_exit_code'] = FALSE;
$shell->execute(
self::COMMAND_KEYTOOL,
'-keystore ' . self::FILE_KEYSTORE . ' ' .
'-delete -alias ' . $certificate . ' ' .
'-storepass ' . self::CONSTANT_KEYSTORE_PW,
TRUE,
$options
);
} catch (\Exception $e) {
// Not fatal
}
$shell->execute(
self::COMMAND_KEYTOOL,
'-importkeystore ' .
'-alias ' . $certificate . ' ' .
'-destkeystore ' . self::FILE_KEYSTORE . ' ' .
'-deststorepass changeit ' .
'-destkeypass changeit ' .
'-srcstoretype PKCS12 ' .
'-srcstorepass changeit ' .
'-srckeystore ' . self::FILE_PKCS12,
TRUE
);
// TODO: Hotdeploy
/*
$details = $certificate_manager->get_certificate($certificate);
$cert_files = ['certificate-filename', 'key-filename'];
foreach ($cert_files as $cert_file) {
$source = $details[$cert_file];
$basename = basename($source);
$destination = self::PATH_HOTDEPLOY . '/' . $cert_file . '.pem';
$destination_file = new File($destination);
if ($destination_file->exists())
$destination_file->delete();
$file = new File($source);
$file->copy_to($destination, FALSE);
$destination_file->chown(self::OPENFIRE_USER, self::OPENFIRE_GROUP);
$destination_file->chmod('0660');
}
*/
}
/**
* Sets install defaults.
*
* On install, a few defaults are guessed based on existing ClearOS
* configuration. For instance, we can use the default internet hostname
* to sett the XMPP domain.
*
* @return void
* @throws Engine_Exception
*/
public function set_install_defaults()
{
clearos_profile(__METHOD__, __LINE__);
// Update domain and hostname information
//---------------------------------------
$hostname = new Hostname();
$xmpp_fqdn = $hostname->get_internet_hostname();
$this->_set_property(self::PROPERTY_XMPP_FQDN, $xmpp_fqdn);
}
/**
* Sets XMPP domain.
*
* @return void
* @throws Engine_Exception
*/
public function set_xmpp_domain($domain)
{
clearos_profile(__METHOD__, __LINE__);
Validation_Exception::is_valid($this->validate_xmpp_domain($domain));
$this->_set_property(self::PROPERTY_XMPP_DOMAIN, $domain);
// The admin ID needs the domain portion replace
$admin = $this->get_admin();
if (!empty($admin)) {
$new_admin = preg_replace('/@.*/', '@' . $domain, $admin);
$this->set_admin($new_admin);
}
// And so does the ofmeet focus user.
$this->_set_property('org.jitsi.videobridge.ofmeet.focus.user.jid', self::DEFAULT_OFMEET_USER . '@' . $domain);
}
/**
* Sets XMPP fully-qualified hostname.
*
* @return void
* @throws Engine_Exception
*/
public function set_xmpp_fqdn($fqdn)
{
clearos_profile(__METHOD__, __LINE__);
Validation_Exception::is_valid($this->validate_xmpp_fqdn($fqdn));
$this->_set_property(self::PROPERTY_XMPP_FQDN, $fqdn);
}
/**
* Updates properties when underlying configuration changes.
*
* If an adminstrator makes changes to the ClearOS system, some
* properties need to be updated. For example, if the underlying LDAP
* base DN changes, the ldap.adminDN property needs to be updated.
*
* @return void
* @throws Engine_Exception
*/
public function update_ldap_properties()
{
clearos_profile(__METHOD__, __LINE__);
// Update LDAP base DN and search filter
//--------------------------------------
// TODO: add Active Directory support for search_filter
$ldap = LDAP_Factory::create();
$base_dn = $ldap->get_base_dn();
$bind_pw = $ldap->get_bind_password();
$admin_dn = 'cn=manager,ou=Internal,' . $base_dn;
$search_filter = '(|(memberof=cn=openfire_plugin,ou=Groups,ou=Accounts,' . $base_dn . ')(uid=openfire-focus))';
$this->_set_property('ldap.baseDN', $base_dn);
$this->_set_property('ldap.searchFilter', $search_filter);
$this->_set_property('ldap.adminDN', $admin_dn);
$this->_set_property('ldap.adminPassword', $bind_pw);
$this->_set_property('ldap.groupSearchFilter', '(&(objectClass=groupOfNames)(gidNumber<=100000)(!(cn=*_plugin)))');
$this->_set_property('provider.auth.className', 'org.jivesoftware.openfire.ldap.LdapAuthProvider');
// Update adminDN and adminPassword via openfire.xml
//--------------------------------------------------
// TODO: more Active Directory changes required
$this->_reset_security_config();
// $this->_set_xml('adminDN', $admin_dn);
// $this->_set_xml('adminPassword', $bind_pw);
// Restart Openfire
//-----------------
$this->reset(FALSE);
}
/**
* Updates ofmeet focus user.
*
* The ofmeet plugin requires a special user.
*
* @return void
* @throws Engine_Exception
*/
public function update_ofmeet_properties()
{
clearos_profile(__METHOD__, __LINE__);
$password = User_Utilities::generate_password();
$xmpp_domain = $this->get_xmpp_domain();
$this->_set_property('org.jitsi.videobridge.ofmeet.focus.user.jid', self::DEFAULT_OFMEET_USER . '@' . $xmpp_domain);
$this->_set_property('org.jitsi.videobridge.ofmeet.focus.user.password', $password);
$user = User_Factory::create(self::DEFAULT_OFMEET_USER);
if ($user->exists()) {
$user->reset_password($password, $password, 'openfire', FALSE);
} else {
clearos_log('openfire', 'creating focus user');
$user_info['core']['first_name'] = 'Openfire';
$user_info['core']['last_name'] = 'Focus User';
$user_info['core']['type'] = 'built-in';
$user_info['core']['home_directory'] = '/var/clearos/openfire/focus-user';
$user_info['plugins']['openfire']['state'] = TRUE;
$user->add($user_info, $password);
}
}
///////////////////////////////////////////////////////////////////////////////
// V A L I D A T I O N M E T H O D S
///////////////////////////////////////////////////////////////////////////////
/**
* Validation routine for digital certificates.
*
* @param string $certificate basename of digital certificate
*
* @return string error message if certificate is invalid
*/
public function validate_certificate($certificate)
{
clearos_profile(__METHOD__, __LINE__);
$certificate_manager = new Certificate_Manager();
$certificates = $certificate_manager->get_certificates();
if (!array_key_exists($certificate, $certificates))
return lang('certificate_manager_certificate_invalid');
}
/**
* Validation routine for XMPP domain.
*
* @param string $ip IP address
*
* @return string error message if domain is invalid
*/
public function validate_xmpp_domain($domain)
{
clearos_profile(__METHOD__, __LINE__);
if (! Network_Utils::is_valid_domain($domain))
return lang('network_domain_invalid');
}
/**
* Validation routine for XMPP fully-qualified domain.
*
* @param string $ip IP address
*
* @return string error message if FQDN is invalid
*/
public function validate_xmpp_fqdn($fqdn)
{
clearos_profile(__METHOD__, __LINE__);
if (! Network_Utils::is_valid_hostname($fqdn))
return lang('network_hostname_invalid');
}
/**
* Validation routine for username.
*
* @param string $ip IP address
*
* @return string error message if username is invalid
*/
public function validate_username($username)
{
clearos_profile(__METHOD__, __LINE__);
$all_users = $this->get_possible_admins();
if (!in_array($username, $all_users))
return lang('openfire_administrator_account_invalid');
}
///////////////////////////////////////////////////////////////////////////////
// P R I V A T E M E T H O D S
///////////////////////////////////////////////////////////////////////////////
/**
* Returns value of given property.
*
* @param string $property property
*
* @return string value of given property
* @throws Engine_Exception
*/
protected function _get_property($property)
{
clearos_profile(__METHOD__, __LINE__);
$system_database = new System_Database();
$results = $system_database->run_query(self::DATABASE, 'SELECT name,propValue FROM ofProperty');
$value = '';
foreach ($results as $result) {
if ($property == $result['name'])
$value = $result['propValue'];
}
return $value;
}
/**
* Sets value for a given property.
*
* @param string $property property
* @param string $value value
*
* @return void
* @throws Engine_Exception
*/
protected function _set_property($property, $value)
{
clearos_profile(__METHOD__, __LINE__);
$system_database = new System_Database();
$query = "INSERT INTO ofProperty (name,propValue) " .
"VALUES ('$property','$value') " .
"ON DUPLICATE KEY UPDATE name = VALUES(name), propValue = VALUES(propValue);";
$system_database->run_update(self::DATABASE, $query);
}
/**
* Resets security configuration back to default.
*
* The security.xml parameters disappear. Copy fresh default when needed.
*
* @return void
* @throws Engine_Exception
*/
protected function _reset_security_config()
{
clearos_profile(__METHOD__, __LINE__);
// TODO: just inject the LDAP parameters instead of stomping
// on security.xml.
$file = new File(clearos_app_base('openfire') . '/deploy/security.xml');
$file->copy_to(self::FILE_SECURITY_CONFIG);
$file = new File(self::FILE_SECURITY_CONFIG);
$file->chown('openfire', 'openfire');
}
/**
* Sets special LDAP values in openfire.xml.
*
* @param string $property property
* @param string $value value
*
* @return void
* @throws Engine_Exception
*/
protected function _set_xml($property, $value)
{
clearos_profile(__METHOD__, __LINE__);
// TODO: pull in webconfig-php-xml parser in a future release.
// For now, just hack it in so we can avoid a webconfig restart.
$file = new File(self::FILE_CONFIG);
$contents = $file->get_contents_as_array();
$ldap_found = FALSE;
$in_ldap = FALSE;
$new = [];
foreach ($contents as $line) {
if ($in_ldap && (preg_match('/<' . $property . '>/', $line)))
continue;
$new[] = $line;
if (preg_match('/<ldap>/', $line)) {
$new[] = ' <' . $property . '>' . $value . '</' . $property . '>';
$ldap_found = TRUE;
$in_ldap = TRUE;
}
}
if (!$ldap_found) {
$new = [];
foreach ($contents as $line) {
if (preg_match('/<\/jive>/', $line)) {
$new[] = ' <ldap>';
$new[] = ' <' . $property . '>' . $value . '</' . $property . '>';
$new[] = ' </ldap>';
}
$new[] = $line;
}
}
if ($file->exists())
$file->delete();
$file->create('openfire', 'openfire', '0644');
$file->add_lines(implode("\n", $new));
}
}