Passed
Push — master ( 09cfdd...94b89f )
by Stefan
07:21 queued 03:30
created

Device_Linux::writeConfigVars()   D

Complexity

Conditions 10
Paths 32

Size

Total Lines 39
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 32
nc 32
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/* * ********************************************************************************
4
 * (c) 2011-15 GÉANT on behalf of the GN3, GN3plus and GN4 consortia
5
 * License: see the LICENSE file in the root directory
6
 * ********************************************************************************* */
7
8
/**
9
 * This file creates Linux installers
10
 *
11
 * @author Tomasz Wolniewicz <[email protected]>
12
 * @author Michał Gasewicz <[email protected]> (Network Manager support)
13
 *
14
 * @package ModuleWriting
15
 */
16
namespace devices\linux;
17
use Exception;
18
/**
19
 * This class creates Linux installers. It supports NetworkManager and raw
20
 * wpa_supplicant files.
21
 *
22
 * @author Tomasz Wolniewicz <[email protected]>
23
 * @author Michał Gasewicz <[email protected]> (Network Manager support)
24
 *
25
 * @package ModuleWriting
26
 */
27
class Device_Linux extends \core\DeviceConfig {
28
29
    final public function __construct() {
30
        parent::__construct();
31
        $this->setSupportedEapMethods([\core\common\EAP::EAPTYPE_PEAP_MSCHAP2, \core\common\EAP::EAPTYPE_TTLS_PAP, \core\common\EAP::EAPTYPE_TTLS_MSCHAP2, \core\common\EAP::EAPTYPE_TLS, \core\common\EAP::EAPTYPE_SILVERBULLET]);
32
    }
33
34
    public function writeInstaller() {
35
        $installerPath = $this->installerBasename . ".py";
36
        $this->copyFile("main.py", $installerPath);
37
        $installer = fopen($installerPath,"a");
38
        if ($installer === FALSE) {
39
            throw new Exception("Unable to open installer file for writing!");
40
        }
41
        fwrite($installer,$this->writeMessages());
42
        fwrite($installer,$this->writeConfigVars());
43
        fwrite($installer, "run_installer()\n");
44
        fclose($installer);
45
        return($installerPath);
46
    }
47
48
    public function writeDeviceInfo() {
49
        $ssidCount = count($this->attributes['internal:SSID']);
50
        $out = '';
51
52
        $out .= _("The installer is in the form of a bash script. It will try to configure eduroam under Network Manager and if this is either not appropriate for your system or your version of Network Manager is too old, a wpa_supplicant config file will be created instead.");
53
        $out .= "<p>";
54
        if ($ssidCount > 1) {
55
            if ($ssidCount > 2) {
56
                $out .= sprintf(_("In addition to <strong>%s</strong> the installer will also configure access to the following networks:"), implode(', ', CONFIG_CONFASSISTANT['CONSORTIUM']['ssid'])) . " ";
57
            } else {
58
                $out .= sprintf(_("In addition to <strong>%s</strong> the installer will also configure access to:"), implode(', ', CONFIG_CONFASSISTANT['CONSORTIUM']['ssid'])) . " ";
59
            }
60
            $iterator = 0;
61
            foreach ($this->attributes['internal:SSID'] as $ssid => $v) {
62
                if (!in_array($ssid, CONFIG_CONFASSISTANT['CONSORTIUM']['ssid'])) {
63
                    if ($iterator > 0) {
64
                        $out .= ", ";
65
                    }
66
                    $iterator++;
67
                    $out .= "<strong>$ssid</strong>";
68
                }
69
            }
70
            $out .= "<p>";
71
        }
72
        $out .= _("The installer will create .cat_installer sub-directory in your home directory and will copy your server certificates there.");
73
        if ($this->selectedEap == \core\common\EAP::EAPTYPE_TLS) {
74
            $out .= sprintf(_("In order to connect to the network you will need a personal certificate in the form of a p12 file. You should obtain this certificate from your %s. Consult the support page to find out how this certificate can be obtained. Such certificate files are password protected. You should have both the file and the password available during the installation process. Your p12 file will also be copied to the .cat_installer directory."), $this->nomenclature_inst);
75
        } elseif ($this->selectedEap != \core\common\EAP::EAPTYPE_SILVERBULLET) {
76
            $out .= sprintf(_("In order to connect to the network you will need an account from your %s. You should consult the support page to find out how this account can be obtained. It is very likely that your account is already activated."), $this->nomenclature_inst);
77
            $out .= "<p>";
78
            $out .= _("You will be requested to enter your account credentials during the installation. This information will be saved so that you will reconnect to the network automatically each time you are in the range.");
79
        }
80
        // nothing to say if we are doing silverbullet.
81
        $out .= "<p>";
82
        return $out;
83
    }
84
    
85
    private function writeMessages() {
86
        $out = '';
87
        $out .= 'Messages.quit = "' . _("Really quit?") . "\"\n";
88
        $out .= 'Messages.username_prompt = "' . _("enter your userid") . "\"\n";
89
        $out .= 'Messages.enter_password = "' . _("enter password") . "\"\n";
90
        $out .= 'Messages.enter_import_password = "' . _("enter your import password") . "\"\n";
91
        $out .= 'Messages.incorrect_password = "' . _("incorrect password") . "\"\n";
92
        $out .= 'Messages.repeat_password = "' . _("repeat your password") . "\"\n";
93
        $out .= 'Messages.passwords_difffer = "' . _("passwords do not match") . "\"\n";
94
        $out .= 'Messages.installation_finished = "' . _("Installation successful") . "\"\n";
95
        $out .= 'Messages.cat_dir_exisits = "' . _("Directory {} exists; some of its files may be overwritten.") . "\"\n";
96
        $out .= 'Messages.cont = "' . _("Continue?") . "\"\n";
97
        $out .= 'Messages.nm_not_supported = "' . _("This Network Manager version is not supported") . "\"\n";
98
        $out .= 'Messages.cert_error = "' . _("Certificate file not found, looks like a CAT error") . "\"\n";
99
        $out .= 'Messages.unknown_version = "' . _("Unknown version") . "\"\n";
100
        $out .= 'Messages.dbus_error = "' . _("DBus connection problem, a sudo might help") . "\"\n";
101
        $out .= 'Messages.yes = "' . _("Y") . "\"\n";
102
        $out .= 'Messages.no = "' . _("N") . "\"\n";
103
        $out .= 'Messages.p12_filter = "' . _("personal certificate file (p12 or pfx)") . "\"\n";
104
        $out .= 'Messages.all_filter = "' . _("All files") . "\"\n";
105
        $out .= 'Messages.p12_title = "' . _("personal certificate file (p12 or pfx)") . "\"\n";
106
        $out .= 'Messages.save_wpa_conf = "' . _("Network Manager configuration failed, but we may generate a wpa_supplicant configuration file if you wish. Be warned that your connection password will be saved in this file as clear text.") . "\"\n";
107
        $out .= 'Messages.save_wpa_confirm = "' . _("Write the file") . "\"\n";
108
        $out .= 'Messages.wrongUsernameFormat = "' ._("Error: Your username must be of the form 'xxx@institutionID' e.g. '[email protected]'!") . "\"\n";
109
        $out .= 'Messages.wrong_realm = "' . _("Error: your username must be in the form of 'xxx@{}'. Please enter the username in the correct format.") . "\"\n";
110
        $out .= 'Messages.wrong_realm_suffix = "' . _("Error: your username must be in the form of 'xxx@institutionID' and end with '{}'. Please enter the username in the correct format.") . "\"\n";
111
    
112
        return $out;
113
    }
114
    
115
    private function writeConfigVars() {
116
        $eapMethod = \core\common\EAP::eapDisplayName($this->selectedEap);
117
        $out = '';
118
        $out .= 'Config.instname = "' . $this->attributes['general:instname'][0] . '"' . "\n";
119
        $out .= 'Config.profilename = "' . $this->attributes['profile:name'][0] . '"' . "\n";
120
        $contacts = $this->mkSupportContacts();
121
        $out .= 'Config.url = "' . $contacts['url'] . '"' . "\n";
122
        $out .= 'Config.email = "' . $contacts['email'] . '"' . "\n";
123
        $out .= 'Config.title = "' . "eduroam CAT" . "\"\n";
124
        $out .= 'Config.servers = ' . $this->mkSubjectAltNameList() . "\n";
125
        $out .= 'Config.ssids = ' . $this->mkSsidList() . "\n";
126
        $out .= 'Config.del_ssids = ' . $this->mkDelSsidList() . "\n";
127
        $out .= "Config.server_match = '" . $this->glueServerNames() . "'\n";
128
        $out .= "Config.eap_outer = '" . $eapMethod['OUTER'] . "'\n";
129
        $out .= "Config.eap_inner = '" . $eapMethod['INNER'] . "'\n";
130
        if ($this->selectedEap == \core\common\EAP::EAPTYPE_TLS && isset($this->attributes['eap-specific:tls_use_other_id']) && $this->attributes['eap-specific:tls_use_other_id'][0] == 'on') {
131
            $out .= "Config.use_other_tls_id = True\n";
132
        }
133
        else {
134
            $out .= "Config.use_other_tls_id = False\n";
135
        }
136
        $tou = $this->mkUserConsent();
137
        $out .= 'Config.tou = ' . ( $tou ? '"""' . $tou . '"""' : 'None' ) . "\n"; 
138
        $out .= 'Config.CA = """' . $this->mkCAfile()  . '"""' . "\n";
139
        $out .= "Config.anonymous_identity = '" . $this->determineOuterIdString() . "'\n";
140
        $out .= 'Config.init_info = """' . $this->mkIntro() . '"""' . "\n";
141
        $out .= 'Config.init_confirmation = "' . $this->mkProfileConfirmation() . "\"\n";
142
        
143
        $out .= 'Config.sb_user_file = """' . $this->mkSbUserFile() . '"""' . "\n";
144
        if (!empty($this->attributes['internal:realm'][0])) {
145
           $out .= 'Config.user_realm = "' . $this->attributes['internal:realm'][0] . "\"\n";
146
        }
147
        if(!empty($this->attributes['internal:hint_userinput_suffix'][0]) && $this->attributes['internal:hint_userinput_suffix'][0] == 1) {
148
            $out .= "Config.hint_user_input = True\n";
149
        }
150
        if(!empty($this->attributes['internal:verify_userinput_suffix'][0]) && $this->attributes['internal:verify_userinput_suffix'][0] == 1) {
151
            $out .= "Config.verify_user_realm_input = True\n";
152
        }        
153
        return $out;
154
    }
155
156
    
157
    private function glueServerNames() {
158
        $serverList = $this->attributes['eap:server_name'];        
159
        if (!$serverList) {
160
            return '';
161
        }
162
        $A0 = array_reverse(explode('.', array_shift($serverList)));
163
        $B = $A0;
164
        foreach ($serverList as $oneServer) {
165
            $A = array_reverse(explode('.', $oneServer));
166
            $B = array_intersect_assoc($A0, $A);
167
            $A0 = $B;
168
        }
169
        return(implode('.', array_reverse($B)));
170
    }
171
172
    private function mkSupportContacts() {
173
        $url = (!empty($this->attributes['support:url'][0])) ? $this->attributes['support:url'][0] : $this->support_url_substitute;
174
        $email = (!empty($this->attributes['support:email'][0])) ? $this->attributes['support:email'][0] : $this->support_email_substitute;
175
        return(['url'=>$url, 'email'=>$email]);
176
    }   
177
    
178
    private function mkSubjectAltNameList() {
179
        $serverList = $this->attributes['eap:server_name'];
180
        if (!$serverList) {
181
            return '';
182
        }
183
        $out = '';
184
        foreach ($serverList as $oneServer) {
185
            if ($out) {
186
                $out .= ',';
187
            }
188
            $out .= "'DNS:$oneServer'";
189
        }
190
        return "[" . $out. "]";
191
    }
192
193
    
194
    private function mkSsidList() {
195
        $ssids = $this->attributes['internal:SSID'];
196
        $outArray = [];
197
        foreach ($ssids as $ssid => $cipher) {
198
            $outArray[] = "'$ssid'";
199
        }
200
        return '[' . implode(', ', $outArray) . ']';
201
    }
202
    
203
    private function mkDelSsidList() {
204
        $outArray = [];
205
        $delSSIDs = $this->attributes['internal:remove_SSID'];
206
        foreach ($delSSIDs as $ssid => $cipher) {
207
            if ($cipher == 'DEL') {
208
                $outArray[] = "'$ssid'";
209
            }
210
        }
211
        return '[' . implode(', ', $outArray) . ']';
212
    }
213
    
214
    private function mkCAfile(){
215
        $out = '';
216
        $cAlist = $this->attributes['internal:CAs'][0];
217
        foreach ($cAlist as $oneCa) {
218
            $out .= $oneCa['pem'] . "\n";
219
        }
220
        return $out;
221
    }
222
    
223
    private function mkIntro() {
224
        $out = _("This installer has been prepared for {0}") . '\n\n' . _("More information and comments:") . '\n\nEMAIL: {1}\nWWW: {2}\n\n' .
225
            _("Installer created with software from the GEANT project.") . "\"\n";
226
        return $out;
227
    }
228
    
229
    private function mkUserConsent() {
230
        $out = '';
231
        if (isset($this->attributes['support:info_file'])) {
232
            if ($this->attributes['internal:info_file'][0]['mime'] == 'txt') {
233
                $out = $this->attributes['support:info_file'][0];
234
            }
235
        }
236
        return $out;
237
    }
238
    
239
    private function mkProfileConfirmation() {
240
        if ($this->attributes['internal:profile_count'][0] > 1) {
241
            $out = _("This installer will only work properly if you are a member of {0} and the user group: {1}.");
242
        } else {
243
            $out = _("This installer will only work properly if you are a member of {0}.");
244
        }
245
        return $out;
246
    }
247
    
248
249
    
250
    private function mkSbUserFile() {
251
        if ($this->selectedEap == \core\common\EAP::EAPTYPE_SILVERBULLET) {
252
            return chunk_split(base64_encode($this->clientCert["certdata"]), 64, "\n");
253
        }
254
        return "";
255
    }
256
    
257
}
258