Passed
Push — release_2_0 ( b90bc6...d7aaf5 )
by Stefan
08:26 queued 12s
created

MobileconfigSuperclass::removenetworkBlock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 31
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/*
3
 * *****************************************************************************
4
 * Contributions to this work were made on behalf of the GÉANT project, a 
5
 * project that has received funding from the European Union’s Framework 
6
 * Programme 7 under Grant Agreements No. 238875 (GN3) and No. 605243 (GN3plus),
7
 * Horizon 2020 research and innovation programme under Grant Agreements No. 
8
 * 691567 (GN4-1) and No. 731122 (GN4-2).
9
 * On behalf of the aforementioned projects, GEANT Association is the sole owner
10
 * of the copyright in all material which was developed by a member of the GÉANT
11
 * project. GÉANT Vereniging (Association) is registered with the Chamber of 
12
 * Commerce in Amsterdam with registration number 40535155 and operates in the 
13
 * UK as a branch of GÉANT Vereniging.
14
 * 
15
 * Registered office: Hoekenrode 3, 1102BR Amsterdam, The Netherlands. 
16
 * UK branch address: City House, 126-130 Hills Road, Cambridge CB2 1PQ, UK
17
 *
18
 * License: see the web/copyright.inc.php file in the file structure or
19
 *          <base_url>/copyright.php after deploying the software
20
 */
21
22
/**
23
 * This file contains the installer for iOS devices and Apple 10.7 Lion
24
 *
25
 *
26
 * @author Stefan Winter <[email protected]>
27
 * @package Developer
28
 */
29
30
namespace devices\apple_mobileconfig;
31
32
use \Exception;
33
34
/**
35
 * This is the main implementation class of the module
36
 *
37
 * The class should only define one public method: writeInstaller.
38
 *
39
 * All other methods and properties should be private. This example sets zipInstaller method to protected, so that it can be seen in the documentation.
40
 *
41
 * @package Developer
42
 */
43
abstract class MobileconfigSuperclass extends \core\DeviceConfig {
44
45
    private $instName;
46
    private $profileName;
47
    private $massagedInst;
48
    private $massagedProfile;
49
    private $massagedCountry;
50
    private $massagedConsortium;
51
    private $lang;
52
    static private $iPhonePayloadPrefix = "org.1x-config";
53
    private $clientCertUUID;
54
55
    /**
56
     * construct with the standard set of EAP methods we support, and preload
57
     * specialities
58
     */
59
    public function __construct() {
60
        parent::__construct();
61
        \core\common\Entity::intoThePotatoes();
62
        // that's what all variants support. Sub-classes can change it.
63
        $this->setSupportedEapMethods([\core\common\EAP::EAPTYPE_PEAP_MSCHAP2, \core\common\EAP::EAPTYPE_TTLS_PAP, \core\common\EAP::EAPTYPE_TTLS_MSCHAP2, \core\common\EAP::EAPTYPE_SILVERBULLET]);
64
        $this->specialities['internal:verify_userinput_suffix'] = _("It is not possible to actively verify the user input for suffix match; but if there is no 'Terms of Use' configured, the installer will display a corresponding hint to the user instead.");
65
        \core\common\Entity::outOfThePotatoes();
66
    }
67
68
    /**
69
     * massage a name so that it becomes acceptable inside the plist XML
70
     * 
71
     * @param string $input the literal name
72
     * @return string
73
     */
74
    private function massageName($input) {
75
        return htmlspecialchars(strtolower(iconv("UTF-8", "US-ASCII//TRANSLIT", preg_replace(['/ /', '/\//'], '_', $input))), ENT_XML1, 'UTF-8');
76
    }
77
78
    /**
79
     * the general part of a mobileconfig file in plist format
80
     * @return string
81
     */
82
    private function generalPayload() {
83
        \core\common\Entity::intoThePotatoes();
84
        $tagline = sprintf(_("Network configuration profile '%s' of '%s' - provided by %s"), htmlspecialchars($this->profileName, ENT_XML1, 'UTF-8'), htmlspecialchars($this->instName, ENT_XML1, 'UTF-8'), CONFIG_CONFASSISTANT['CONSORTIUM']['display_name']);
85
86
        $eapType = $this->selectedEap;
87
        // simpler message for silverbullet
88
        if ($eapType['INNER'] == \core\common\EAP::NE_SILVERBULLET) {
89
            $tagline = sprintf(_("%s configuration for IdP '%s' - provided by %s"), \core\ProfileSilverbullet::PRODUCTNAME, htmlspecialchars($this->instName, ENT_XML1, 'UTF-8'), CONFIG_CONFASSISTANT['CONSORTIUM']['display_name']);
90
        }
91
92
        $retval = "
93
      <key>PayloadDescription</key>
94
         <string>$tagline</string>
95
      <key>PayloadDisplayName</key>
96
         <string>" . CONFIG_CONFASSISTANT['CONSORTIUM']['display_name'] . "</string>
97
      <key>PayloadIdentifier</key>
98
         <string>" . self::$iPhonePayloadPrefix . ".$this->massagedConsortium.$this->massagedCountry.$this->massagedInst.$this->massagedProfile.$this->lang</string>
99
      <key>PayloadOrganization</key>
100
         <string>" . htmlspecialchars(iconv("UTF-8", "UTF-8//IGNORE", $this->attributes['general:instname'][0]), ENT_XML1, 'UTF-8') . ( $this->attributes['internal:profile_count'][0] > 1 ? " (" . htmlspecialchars(iconv("UTF-8", "UTF-8//IGNORE", $this->attributes['profile:name'][0]), ENT_XML1, 'UTF-8') . ")" : "") . "</string>
101
      <key>PayloadType</key>
102
         <string>Configuration</string>
103
      <key>PayloadUUID</key>
104
         <string>" . \core\common\Entity::uuid('', self::$iPhonePayloadPrefix . $this->massagedConsortium . $this->massagedCountry . $this->massagedInst . $this->massagedProfile) . "</string>
105
      <key>PayloadVersion</key>
106
         <integer>1</integer>";
107
        \core\common\Entity::outOfThePotatoes();
108
        return $retval;
109
    }
110
111
    const FILE_START = "<?xml version=\"1.0\" encoding=\"utf-8\"?>
112
<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\"
113
\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
114
<plist version=\"1.0\">
115
<dict>";
116
    const FILE_END = "</dict></plist>";
117
    const BUFFER_CONSENT_PRE = "
118
      <key>ConsentText</key>
119
         <dict>
120
            <key>default</key>
121
               <string>";
122
    const BUFFER_CONSENT_POST = "</string>
123
         </dict>
124
         ";
125
126
    /**
127
     * creates a ConsentText block if either Terms of Use are specified or the
128
     * user input hints should be displayed. Otherwise, produces nothing.
129
     * 
130
     * @return string
131
     */
132
    protected function consentBlock() {
133
        \core\common\Entity::intoThePotatoes();
134
        if (isset($this->attributes['support:info_file'])) {
135
            return MobileconfigSuperclass::BUFFER_CONSENT_PRE . htmlspecialchars(iconv("UTF-8", "UTF-8//TRANSLIT", $this->attributes['support:info_file'][0]), ENT_XML1, 'UTF-8') . MobileconfigSuperclass::BUFFER_CONSENT_POST;
136
        }
137
        if ($this->attributes['internal:verify_userinput_suffix'][0] != 0) {
138
            if (strlen($this->attributes['internal:realm'][0]) > 0) {
139
                $retval =MobileconfigSuperclass::BUFFER_CONSENT_PRE . sprintf(_("Important Notice: your username must end with @%s!"), $this->attributes['internal:realm'][0]) . MobileconfigSuperclass::BUFFER_CONSENT_POST;
140
                \core\common\Entity::outOfThePotatoes();
141
                return $retval;
142
            }
143
            $retval = MobileconfigSuperclass::BUFFER_CONSENT_PRE . _("Important Notice: your username MUST be in the form of xxx@yyy where the yyy is a common suffix identifying your Identity Provider. Please find out what to use there and enter the username in the correct format.") . MobileconfigSuperclass::BUFFER_CONSENT_POST;
144
            \core\common\Entity::outOfThePotatoes();
145
            return $retval;
146
        }
147
        \core\common\Entity::outOfThePotatoes();
148
        return "";
149
    }
150
151
    /**
152
     * create the actual installer XML file
153
     * 
154
     * @return string filename of the generated installer
155
     *
156
     */
157
    public function writeInstaller() {
158
        \core\common\Entity::intoThePotatoes();
159
        $dom = textdomain(NULL);
160
        textdomain("devices");
161
162
        $this->loggerInstance->debug(4, "mobileconfig Module Installer start\n");
163
164
        // remove spaces and slashes (filename!), make sure it's simple ASCII only, then lowercase it
165
        // also escape htmlspecialchars
166
        // not all names and profiles have a name, so be prepared
167
168
        $this->loggerInstance->debug(5, "List of available attributes: " . var_export($this->attributes, TRUE));
169
170
        $this->instName = $this->attributes['general:instname'][0] ?? _("Unnamed Organisation");
171
        $this->profileName = $this->attributes['profile:name'][0] ?? _("Unnamed Profile");
172
173
        $this->massagedInst = $this->massageName($this->instName);
174
        $this->massagedProfile = $this->massageName($this->profileName);
175
        $this->massagedCountry = $this->massageName($this->attributes['internal:country'][0]);
176
        $this->massagedConsortium = $this->massageName(CONFIG_CONFASSISTANT['CONSORTIUM']['name']);
177
        $this->lang = preg_replace('/\..+/', '', setlocale(LC_ALL, "0"));
178
179
        $eapType = $this->selectedEap;
180
181
        $outputXml = self::FILE_START;
182
        $outputXml .= "<key>PayloadContent</key>
183
         <array>";
184
185
        // if we are in silverbullet, we will need a whole own block for the client credential
186
        // and also for the profile expiry
187
188
        $this->clientCertUUID = NULL;
189
        if ($eapType['INNER'] == \core\common\EAP::NE_SILVERBULLET) {
190
            $blockinfo = $this->clientP12Block();
191
            $outputXml .= $blockinfo['block'];
192
            $this->clientCertUUID = $blockinfo['UUID'];
193
        }
194
195
        $outputXml .= $this->allCA();
196
197
        $outputXml .= $this->allNetworkBlocks();
198
199
        $outputXml .= "</array>";
200
        $outputXml .= $this->generalPayload();
201
        $outputXml .= $this->consentBlock();
202
203
        if ($eapType['INNER'] == \core\common\EAP::NE_SILVERBULLET) {
204
            $outputXml .= $this->expiryBlock();
205
        }
206
        $outputXml .= self::FILE_END;
207
208
        file_put_contents('installer_profile', $outputXml);
209
210
        textdomain($dom);
211
212
        $fileName = $this->installerBasename . '.mobileconfig';
213
214
        if (!$this->sign) {
215
            rename("installer_profile", $fileName);
216
            \core\common\Entity::outOfThePotatoes();
217
            return $fileName;
218
        }
219
        // still here? Then we are signing.
220
        $signing = system($this->sign . " installer_profile '$fileName' > /dev/null");
221
        if ($signing === FALSE) {
222
            $this->loggerInstance->debug(2, "Signing the mobileconfig installer $fileName FAILED!\n");
223
        }
224
        \core\common\Entity::outOfThePotatoes();
225
        return $fileName;
226
    }
227
228
    /**
229
     * produces the HTML text to be displayed when clicking on the "help" button
230
     * besides the download button.
231
     * 
232
     * @return string
233
     */
234
    public function writeDeviceInfo() {
235
        \core\common\Entity::intoThePotatoes();
236
        $ssidCount = count($this->attributes['internal:SSID']);
237
        $certCount = count($this->attributes['internal:CAs'][0]);
238
        $out = "<p>" . _("For best results, please use the built-in browser (Safari) to open the configuration file.") . "</p>";
239
        $out .= "<p>";
240
        $out .= _("The profile will install itself after you click (or tap) the button. You will be asked for confirmation/input at several points:");
241
        $out .= "<ul>";
242
        $out .= "<li>" . _("to install the profile") . "</li>";
243
        $out .= "<li>" . ngettext("to accept the server certificate authority", "to accept the server certificate authorities", $certCount);
244
        if ($certCount > 1) {
245
            $out .= " " . sprintf(_("(%d times)"), $certCount);
246
        }
247
        $out .= "</li>";
248
        $out .= "<li>" . _("to enter the username and password you have been given by your organisation");
249
        if ($ssidCount > 1) {
250
            $out .= " " . sprintf(_("(%d times each, because %s is installed for %d SSIDs)"), $ssidCount, CONFIG_CONFASSISTANT['CONSORTIUM']['display_name'], $ssidCount);
251
        }
252
        $out .= "</li>";
253
        $out .= "</ul>";
254
        $out .= "</p>";
255
        \core\common\Entity::outOfThePotatoes();
256
        return $out;
257
    }
258
259
    /**
260
     * collates a list of the UUIDs of all the CAs which are to be included in
261
     * the mobileconfig file
262
     * 
263
     * @return array
264
     */
265
    private function listCAUuids() {
266
        $retval = [];
267
        foreach ($this->attributes['internal:CAs'][0] as $ca) {
268
            $retval[] = $ca['uuid'];
269
        }
270
        return $retval;
271
    }
272
273
    /**
274
     * This is the XML structure subtree of a Network block which contains the
275
     * settings specific to Passpoint
276
     * 
277
     * @param array $consortiumOi list of consortiumOi to put into structure
278
     * @return string
279
     */
280
    private function passPointBlock($consortiumOi) {
281
        $retval = "
282
               <key>IsHotspot</key>
283
               <true/>
284
               <key>ServiceProviderRoamingEnabled</key>
285
               <true/>
286
               <key>DisplayedOperatorName</key>
287
               <string>" . CONFIG_CONFASSISTANT['CONSORTIUM']['display_name'] . " via Passpoint</string>";
288
        // if we don't know the realm, omit the entire DomainName key
289
        if (isset($this->attributes['internal:realm'])) {
290
            $retval .= "<key>DomainName</key>
291
               <string>";
292
            $retval .= $this->attributes['internal:realm'][0];
293
            $retval .= "</string>
294
                ";
295
        }
296
        $retval .= "                <key>RoamingConsortiumOIs</key>
297
                <array>";
298
        foreach ($consortiumOi as $oiValue) {
299
            $retval .= "<string>$oiValue</string>";
300
        }
301
        $retval .= "</array>";
302
        // this is an undocmented value found on the net. Does it do something useful?
303
        $retval .= "<key>_UsingHotspot20</key>
304
                <true/>
305
                ";
306
        // do we need to set NAIRealmName ? In Rel 1, probably yes, in Rel 2, 
307
        // no because ConsortiumOI is enough.
308
        // but which release is OS X doing? And what should we fill in, given
309
        // that we have thousands of realms? Try just eduroam.org
310
        // 
311
        // tests from Hideaki suggest it's better not to set it; if Roaming
312
        // consortium OI and NAIRealmNames are both set, connecting to a hotspot
313
        // with just RCOI does not work
314
        /* if (CONFIG_CONFASSISTANT['CONSORTIUM']['name'] == "eduroam") {
315
            $retval .= "<key>NAIRealmNames</key>
316
                <array>
317
                    <string>eduroam.org</string>
318
                </array>";
319
        }*/
320
        return $retval;
321
    }
322
323
    private $serial;
324
    private $removeSerial;
325
    private $caSerial;
326
327
    /**
328
     * produces the EAP sub-block of a Network block
329
     * 
330
     * @param array $eapType EAP type in array notation
331
     * @return string
332
     */
333
    private function eapBlock($eapType) {
334
        $realm = $this->determineOuterIdString();
335
        $retval = "<key>EAPClientConfiguration</key>
336
                  <dict>
337
                      <key>AcceptEAPTypes</key>
338
                         <array>
339
                            <integer>" . $eapType['OUTER'] . "</integer>
340
                         </array>
341
                      <key>EAPFASTProvisionPAC</key>
342
                            <true />
343
                      <key>EAPFASTUsePAC</key>
344
                            <true />
345
                      <key>EAPFastProvisionPACAnonymously</key>
346
                            <false />
347
                      <key>OneTimeUserPassword</key>
348
                            <false />
349
";
350
        if ($realm !== NULL) {
351
            $retval .= "<key>OuterIdentity</key>
352
                                    <string>" . htmlspecialchars($realm, ENT_XML1, 'UTF-8') . "</string>
353
";
354
        }
355
        $retval .= "<key>PayloadCertificateAnchorUUID</key>
356
                         <array>";
357
        foreach ($this->listCAUuids() as $uuid) {
358
            if (in_array($uuid, $this->CAsAccountedFor)) {
359
                $retval .= "
360
<string>$uuid</string>";
361
            }
362
        }
363
        $retval .= "
364
                         </array>
365
                      <key>TLSAllowTrustExceptions</key>
366
                         <false />
367
                      <key>TLSTrustedServerNames</key>
368
                         <array>";
369
        foreach ($this->attributes['eap:server_name'] as $commonName) {
370
            $retval .= "
371
<string>$commonName</string>";
372
        }
373
        $retval .= "
374
                         </array>";
375
        if ($eapType['INNER'] == \core\common\EAP::NE_SILVERBULLET) {
376
            $retval .= "<key>UserName</key><string>" . $this->clientCert["certObject"]->username . "</string>";
377
        }
378
        $retval .= "
379
                      <key>TTLSInnerAuthentication</key>
380
                         <string>" . ($eapType['INNER'] == \core\common\EAP::NONE ? "PAP" : "MSCHAPv2") . "</string>
381
                   </dict>";
382
        return $retval;
383
    }
384
385
    /**
386
     * produces the Proxy sub-block of a Network block
387
     * 
388
     * @return string
389
     */
390
    protected function proxySettings() {
391
        $buffer = "<key>ProxyType</key>";
392
        if (isset($this->attributes['media:force_proxy'])) {
393
            // find the port delimiter. In case of IPv6, there are multiple ':' 
394
            // characters, so we have to find the LAST one
395
            $serverAndPort = explode(':', strrev($this->attributes['media:force_proxy'][0]), 2);
396
            // characters are still reversed, invert on use!
397
            $buffer .= "<string>Manual</string>
398
                  <key>ProxyServer</key>
399
                  <string>" . strrev($serverAndPort[1]) . "</string>
400
                  <key>ProxyServerPort</key>
401
                  <integer>" . strrev($serverAndPort[0]) . "</integer>
402
                  <key>ProxyPACFallbackAllowed</key>
403
                  <false/>";
404
        } else {
405
            $buffer .= "<string>Auto</string>
406
                  <key>ProxyPACFallbackAllowed</key>
407
                  <true/>";
408
        }
409
        return $buffer;
410
    }
411
412
    /**
413
     * produces an entire Network block
414
     * 
415
     * @param int                  $blocktype      which type of network block is this?
416
     * @param string|array|boolean $toBeConfigured variable part of the config. Single SSID or list of ConsortiumOi
417
     * @return string
418
     * @throws Exception
419
     */
420
    private function networkBlock($blocktype, $toBeConfigured) {
421
        \core\common\Entity::intoThePotatoes();
422
        $eapType = $this->selectedEap;
423
        switch ($blocktype) {
424
            case MobileconfigSuperclass::NETWORK_BLOCK_TYPE_SSID:
425
                if (!is_string($toBeConfigured)) {
426
                    throw new Exception("SSID must be a string!");
427
                }
428
                $escapedSSID = htmlspecialchars($toBeConfigured, ENT_XML1, 'UTF-8');
429
                $payloadIdentifier = "wifi." . $this->serial;
430
                $payloadShortName = sprintf(_("SSID %s"), $escapedSSID);
431
                $payloadName = sprintf(_("%s configuration for network name %s"), CONFIG_CONFASSISTANT['CONSORTIUM']['display_name'], $escapedSSID);
432
                $encryptionTypeString = "WPA";
433
                $setupModesString = "";
434
                $wifiNetworkIdentification = "<key>SSID_STR</key>
435
                  <string>$escapedSSID</string>";
436
                break;
437
            case MobileconfigSuperclass::NETWORK_BLOCK_TYPE_WIRED:
438
                if (!is_bool($toBeConfigured)) {
439
                    throw new Exception("We expected a TRUE here!");
440
                }
441
                $payloadIdentifier = "firstactiveethernet";
442
                $payloadShortName = _("Wired Network");
443
                $payloadName = sprintf(_("%s configuration for wired network"), CONFIG_CONFASSISTANT['CONSORTIUM']['display_name']);
444
                $encryptionTypeString = "any";
445
                $setupModesString = "
446
               <key>SetupModes</key>
447
                  <array>
448
                     <string>System</string>
449
                  </array>";
450
                $wifiNetworkIdentification = "";
451
                break;
452
            case MobileconfigSuperclass::NETWORK_BLOCK_TYPE_CONSORTIUMOIS:
453
                if (!is_array($toBeConfigured)) {
454
                    throw new Exception("ConsortiumOI list must be an array!");
455
                }
456
                $payloadIdentifier = "hs20";
457
                $payloadShortName = _("Hotspot 2.0 Settings");
458
                $payloadName = sprintf(_("%s Hotspot 2.0 configuration"), CONFIG_CONFASSISTANT['CONSORTIUM']['display_name']);
459
                $encryptionTypeString = "WPA";
460
                $setupModesString = "";
461
                $wifiNetworkIdentification = $this->passPointBlock($toBeConfigured);
462
                break;
463
            default:
464
                throw new Exception("This type of network block is unknown!");
465
        }
466
        $retval = "<dict>";
467
        $retval .= $this->eapBlock($eapType);
468
        $retval .= "<key>EncryptionType</key>
469
                  <string>$encryptionTypeString</string>
470
               <key>HIDDEN_NETWORK</key>
471
                  <true />
472
               <key>PayloadDescription</key>
473
                  <string>$payloadName</string>
474
               <key>PayloadDisplayName</key>
475
                  <string>$payloadShortName</string>
476
               <key>PayloadIdentifier</key>
477
                  <string>" . self::$iPhonePayloadPrefix . ".$this->massagedConsortium.$this->massagedCountry.$this->massagedInst.$this->massagedProfile.$this->lang.$payloadIdentifier</string>
478
               <key>PayloadOrganization</key>
479
                  <string>" . $this->massagedConsortium . ".1x-config.org</string>
480
               <key>PayloadType</key>
481
                  <string>com.apple." . ($blocktype == MobileconfigSuperclass::NETWORK_BLOCK_TYPE_WIRED ? "firstactiveethernet" : "wifi") . ".managed</string>";
482
        $retval .= $this->proxySettings();
483
        $retval .= $setupModesString;
484
        if ($eapType['INNER'] == \core\common\EAP::NE_SILVERBULLET) {
485
            if ($this->clientCertUUID === NULL) {
486
                throw new Exception("Silverbullet REQUIRES a client certificate and we need to know the UUID!");
487
            }
488
            $retval .= "<key>PayloadCertificateUUID</key>
489
                        <string>$this->clientCertUUID</string>";
490
        }
491
        $retval .= "
492
               <key>PayloadUUID</key>
493
                  <string>" . \core\common\Entity::uuid() . "</string>
494
               <key>PayloadVersion</key>
495
                  <integer>1</integer>
496
                  $wifiNetworkIdentification</dict>";
497
        $this->serial = $this->serial + 1;
498
        \core\common\Entity::outOfThePotatoes();
499
        return $retval;
500
    }
501
502
    /**
503
     * Produces a Network block which sets a network to manual join (we don't
504
     * get any closer to removing a network in mobileconfig)
505
     * 
506
     * @param string $ssid the SSID to set to manual join only
507
     * @return string
508
     */
509
    private function removenetworkBlock($ssid) {
510
        \core\common\Entity::intoThePotatoes();
511
        $retval = "
512
<dict>
513
	<key>AutoJoin</key>
514
	<false/>
515
	<key>EncryptionType</key>
516
	<string>None</string>
517
	<key>HIDDEN_NETWORK</key>
518
	<false/>
519
	<key>IsHotspot</key>
520
	<false/>
521
	<key>PayloadDescription</key>
522
	<string>" . sprintf(_("This SSID should not be used after bootstrapping %s"), CONFIG_CONFASSISTANT['CONSORTIUM']['display_name']) . "</string>
523
	<key>PayloadDisplayName</key>
524
	<string>" . _("Disabled WiFi network") . "</string>
525
	<key>PayloadIdentifier</key>
526
	<string>" . self::$iPhonePayloadPrefix . ".$this->massagedConsortium.$this->massagedCountry.$this->massagedInst.$this->massagedProfile.$this->lang.wifi.disabled.$this->removeSerial</string>
527
	<key>PayloadType</key>
528
	<string>com.apple.wifi.managed</string>
529
	<key>PayloadUUID</key>
530
	<string>" . \core\common\Entity::uuid() . "</string>
531
	<key>PayloadVersion</key>
532
	<real>1</real>";
533
        $retval .= $this->proxySettings();
534
        $retval .= "<key>SSID_STR</key>
535
	<string>$ssid</string>
536
</dict>
537
";
538
        \core\common\Entity::outOfThePotatoes();
539
        return $retval;
540
    }
541
542
    const NETWORK_BLOCK_TYPE_SSID = 100;
543
    const NETWORK_BLOCK_TYPE_CONSORTIUMOIS = 101;
544
    const NETWORK_BLOCK_TYPE_WIRED = 102;
545
546
    /**
547
     * produces the entire series of Network blocks; all for SSID-based, 
548
     * Passpoint-based, wired, and manual-select only SSIDs
549
     * 
550
     * @return string
551
     */
552
    private function allNetworkBlocks() {
553
        $retval = "";
554
        $this->serial = 0;
555
556
        foreach (array_keys($this->attributes['internal:SSID']) as $ssid) {
557
            $retval .= $this->networkBlock(MobileconfigSuperclass::NETWORK_BLOCK_TYPE_SSID, $ssid);
558
        }
559
        if (isset($this->attributes['media:wired']) && get_class($this) == "devices\apple_mobileconfig\Device_mobileconfig_os_x") {
560
            $retval .= $this->networkBlock(MobileconfigSuperclass::NETWORK_BLOCK_TYPE_WIRED, TRUE);
561
        }
562
        if (count($this->attributes['internal:consortia']) > 0) {
563
            $retval .= $this->networkBlock(MobileconfigSuperclass::NETWORK_BLOCK_TYPE_CONSORTIUMOIS, $this->attributes['internal:consortia']);
564
        }
565
        if (isset($this->attributes['media:remove_SSID'])) {
566
            $this->removeSerial = 0;
567
            foreach ($this->attributes['media:remove_SSID'] as $removeSSID) {
568
                $retval .= $this->removenetworkBlock($removeSSID);
569
                $this->removeSerial = $this->removeSerial + 1;
570
            }
571
        }
572
        return $retval;
573
    }
574
575
    /**
576
     * collates a block with all CAs that are to be included in the mobileconfig
577
     * 
578
     * @return string
579
     */
580
    private function allCA() {
581
        $retval = "";
582
        $this->caSerial = 0;
583
        foreach ($this->attributes['internal:CAs'][0] as $ca) {
584
            $retval .= $this->caBlob($ca);
585
            $this->caSerial = $this->caSerial + 1;
586
        }
587
        return $retval;
588
    }
589
590
    /**
591
     * creates a Cert block containing a client certificate (used in SB only)
592
     * @return array the block itself, and the UUID of the certificate
593
     * @throws Exception
594
     */
595
    private function clientP12Block() {
596
        \core\common\Entity::intoThePotatoes();
597
        if (!is_array($this->clientCert)) {
598
            throw new Exception("the client block was called but there is no client certificate!");
599
        }
600
        $binaryBlob = $this->clientCert["certdata_nointermediate"];
601
        $mimeBlob = base64_encode($binaryBlob);
602
        $mimeFormatted = chunk_split($mimeBlob, 52, "\r\n");
603
        $payloadUUID = \core\common\Entity::uuid('', $mimeBlob);
604
        $retArray = ["block" => "<dict>" .
605
            // we don't include the import password. It's displayed on screen, and should be input by the user.
606
            // <key>Password</key>
607
            //   <string>" . $this->clientCert['password'] . "</string>
608
            "<key>PayloadCertificateFileName</key>
609
                     <string>".$this->massagedConsortium.".pfx</string>
610
                  <key>PayloadContent</key>
611
                     <data>
612
$mimeFormatted
613
                     </data>
614
                  <key>PayloadDescription</key>
615
                     <string>MIME Base-64 encoded PKCS#12 Client Certificate</string>
616
                  <key>PayloadDisplayName</key>
617
                     <string>" . _("eduroam user certificate") . "</string>
618
                  <key>PayloadIdentifier</key>
619
                     <string>com.apple.security.pkcs12.$payloadUUID</string>
620
                  <key>PayloadType</key>
621
                     <string>com.apple.security.pkcs12</string>
622
                  <key>PayloadUUID</key>
623
                     <string>$payloadUUID</string>
624
                  <key>PayloadVersion</key>
625
                     <integer>1</integer>
626
                </dict>",
627
            "UUID" => $payloadUUID,];
628
        \core\common\Entity::outOfThePotatoes();
629
        return $retArray;
630
    }
631
632
    /**
633
     * creates an Expiry block. This is only done in SB; the profile expires
634
     * when the client cert expires.
635
     * 
636
     * @return string
637
     * @throws Exception
638
     */
639
    private function expiryBlock() {
640
        if (!is_array($this->clientCert)) {
641
            throw new Exception("the expiry block was called but there is no client certificate!");
642
        }
643
        $expiryTime = new \DateTime($this->clientCert['certObject']->expiry);
644
        return "<key>RemovalDate</key>
645
        <date>" . $expiryTime->format("Y-m-d") . "T" . $expiryTime->format("H:i:s") . "Z</date>";
646
    }
647
648
    private $CAsAccountedFor = [];
649
650
    /**
651
     * creates a block for one single CA
652
     * 
653
     * @param array $ca the CA for which to generate the XML block
654
     * @return string
655
     */
656
    private function caBlob($ca) {
657
        \core\common\Entity::intoThePotatoes();
658
        $stream = "";
659
        if (!in_array($ca['uuid'], $this->CAsAccountedFor)) { // skip if this is a duplicate
660
            // cut lines with CERTIFICATE
661
            $stage1 = preg_replace('/-----BEGIN CERTIFICATE-----/', '', $ca['pem']);
662
            $stage2 = preg_replace('/-----END CERTIFICATE-----/', '', $stage1);
663
            $trimmedPem = trim($stage2);
664
665
            $stream = "
666
            <dict>
667
               <key>PayloadCertificateFileName</key>
668
               <string>" . $ca['uuid'] . ".der</string>
669
               <key>PayloadContent</key>
670
               <data>
671
" . $trimmedPem . "</data>
672
               <key>PayloadDescription</key>
673
               <string>" . sprintf(_("The %s Certification Authority"), \core\common\Entity::$nomenclature_inst) . "</string>
674
               <key>PayloadDisplayName</key>
675
               <string>" . sprintf(_("%s CA"), \core\common\Entity::$nomenclature_inst) . "</string>
676
               <key>PayloadIdentifier</key>
677
               <string>" . self::$iPhonePayloadPrefix . ".$this->massagedConsortium.$this->massagedCountry.$this->massagedInst.$this->massagedProfile.credential.$this->caSerial</string>
678
               <key>PayloadOrganization</key>
679
               <string>" . $this->massagedConsortium . ".1x-config.org</string>
680
               <key>PayloadType</key>
681
               <string>com.apple.security.root</string>
682
               <key>PayloadUUID</key><string>" . $ca['uuid'] . "</string>
683
               <key>PayloadVersion</key>
684
               <integer>1</integer>
685
            </dict>";
686
            $this->CAsAccountedFor[] = $ca['uuid'];
687
        }
688
        \core\common\Entity::outOfThePotatoes();
689
        return $stream;
690
    }
691
692
}
693