not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / solid / networkmanager-0.6 / NetworkManager-dbushelper.cpp
blobe1a16871d2f85d0bc3137fb1c4788067a5167ff4
1 /* This file is part of the KDE project
2 Copyright (C) 2007 Will Stephenson <wstephenson@kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
20 #include "NetworkManager-dbushelper.h"
22 #include <stdlib.h>
23 //#include <wireless.h>
24 // stuff copied from wireless.h so we don't have to include it.
25 // take aim at foot, prepare to fire
26 #define IW_AUTH_ALG_OPEN_SYSTEM 0x00000001
27 #define IW_AUTH_ALG_SHARED_KEY 0x00000002
28 #define IW_AUTH_WPA_VERSION_WPA 0x00000002
29 #define IW_AUTH_WPA_VERSION_WPA2 0x00000004
30 #define IW_AUTH_KEY_MGMT_802_1X 1
31 #define IW_AUTH_KEY_MGMT_PSK 2
32 #define IW_AUTH_CIPHER_NONE 0x00000001
34 #include <NetworkManager/NetworkManager.h>
35 #include <NetworkManager/cipher.h>
36 #include <NetworkManager/cipher-wep-ascii.h>
37 #include <NetworkManager/cipher-wep-hex.h>
38 #include <NetworkManager/cipher-wep-passphrase.h>
39 #include <NetworkManager/cipher-wpa-psk-hex.h>
40 #include <NetworkManager/cipher-wpa-psk-passphrase.h>
41 #include <QtDBus>
43 #include <kdebug.h>
44 #include <solid/control/ifaces/authentication.h>
46 QList<QVariant> NMDBusHelper::serialize(Solid::Control::Authentication * auth, const QString & essid, QList<QVariant> & args, bool * error)
48 if (auth)
50 Solid::Control::AuthenticationNone * none = dynamic_cast<Solid::Control::AuthenticationNone *>(auth) ;
51 if (none)
52 return doSerialize(none, essid, args, error);
53 Solid::Control::AuthenticationWep * wep = dynamic_cast<Solid::Control::AuthenticationWep *>(auth) ;
54 if (wep)
55 return doSerialize(wep, essid, args, error);
56 Solid::Control::AuthenticationWpaPersonal * wpap = dynamic_cast<Solid::Control::AuthenticationWpaPersonal *>(auth) ;
57 if (wpap)
58 return doSerialize(wpap, essid, args, error);
59 Solid::Control::AuthenticationWpaEnterprise * wpae = dynamic_cast<Solid::Control::AuthenticationWpaEnterprise *>(auth);
60 if (wpae)
61 return doSerialize(wpae, essid, args, error);
63 *error = true;
64 return QList<QVariant>();
67 QList<QVariant> NMDBusHelper::doSerialize(Solid::Control::AuthenticationNone * none, const QString & essid, QList<QVariant> & args, bool * error)
69 *error = false;
70 args << QVariant(IW_AUTH_CIPHER_NONE);
71 return args;
74 QList<QVariant> NMDBusHelper::doSerialize(Solid::Control::AuthenticationWep * auth, const QString & essid, QList<QVariant> & args, bool * error)
76 // get correct cipher
77 // what's the algorithm for deciding the right cipher to use?
78 *error = false;
79 IEEE_802_11_Cipher * cipher = 0;
80 if (auth->type() == Solid::Control::AuthenticationWep::WepAscii)
82 if (auth->keyLength() == 40 || auth->keyLength() == 64)
83 cipher = cipher_wep64_ascii_new();
84 else if (auth->keyLength() == 104 || auth->keyLength() == 128)
85 cipher = cipher_wep128_ascii_new();
86 else
87 //NM only supports these 2 key lengths, flag an error!
88 *error = true;
90 else if (auth->type() == Solid::Control::AuthenticationWep::WepHex)
92 if (auth->keyLength() == 40 || auth->keyLength() == 64)
93 cipher = cipher_wep64_hex_new();
94 else if (auth->keyLength() == 104 || auth->keyLength() == 128)
95 cipher = cipher_wep128_hex_new();
96 else
97 //NM only supports these 2 key lengths, flag an error!
98 *error = true;
100 else if (auth->type() == Solid::Control::AuthenticationWep::WepPassphrase)
102 if (auth->keyLength() == 40 || auth->keyLength() == 64)
103 cipher = cipher_wep64_passphrase_new();
104 else if (auth->keyLength() == 104 || auth->keyLength() == 128)
105 cipher = cipher_wep128_passphrase_new();
106 else
107 //NM only supports these 2 key lengths, flag an error!
108 *error = true;
110 else
111 // unrecognised auth type, error!
112 *error = true;
113 if (!(*error))
115 // int32 cipher
116 int we_cipher = ieee_802_11_cipher_get_we_cipher(cipher);
117 args << QVariant(we_cipher);
118 // s key
119 // cipher, essid, key
120 char * rawHashedKey = 0;
121 rawHashedKey = ieee_802_11_cipher_hash(cipher, essid.toUtf8(), auth->secrets()["key"].toUtf8());
122 QString hashedKey = QString::fromAscii(rawHashedKey);
123 free(rawHashedKey);
124 args << QVariant(hashedKey);
125 // int32 auth alg
126 if (auth->method() == Solid::Control::AuthenticationWep::WepOpenSystem)
127 args << QVariant(IW_AUTH_ALG_OPEN_SYSTEM);
128 else
129 args << QVariant(IW_AUTH_ALG_SHARED_KEY);
131 if (cipher)
132 kDebug(1441) << "FIXME: delete cipher object";
134 return args;
137 QList<QVariant> NMDBusHelper::doSerialize(Solid::Control::AuthenticationWpaPersonal * auth, const QString & essid, QList<QVariant> & args, bool * error)
139 *error = false;
140 IEEE_802_11_Cipher * cipher = 0;
141 IEEE_802_11_Cipher * hexCipher = 0;
142 IEEE_802_11_Cipher * ppCipher = 0;
143 hexCipher = cipher_wpa_psk_hex_new();
144 ppCipher = cipher_wpa_psk_passphrase_new();
145 QString rawKey = auth->secrets()["key"];
147 // cipher identification algorithm
148 // can be either hex or passphrase
149 // we try both methods
151 // cipher needs the cipher setting on it
152 // which is the protocol
154 switch (auth->protocol())
156 case Solid::Control::AuthenticationWpaPersonal::WpaTkip:
157 cipher_wpa_psk_hex_set_we_cipher(hexCipher, NM_AUTH_TYPE_WPA_PSK_TKIP);
158 cipher_wpa_psk_passphrase_set_we_cipher(ppCipher, NM_AUTH_TYPE_WPA_PSK_TKIP);
159 break;
160 case Solid::Control::AuthenticationWpaPersonal::WpaCcmpAes:
161 cipher_wpa_psk_hex_set_we_cipher(hexCipher, NM_AUTH_TYPE_WPA_PSK_CCMP);
162 cipher_wpa_psk_passphrase_set_we_cipher(ppCipher, NM_AUTH_TYPE_WPA_PSK_CCMP);
163 break;
164 case Solid::Control::AuthenticationWpaPersonal::WpaAuto:
165 default:
166 cipher_wpa_psk_hex_set_we_cipher(hexCipher, NM_AUTH_TYPE_WPA_PSK_AUTO);
167 cipher_wpa_psk_passphrase_set_we_cipher(ppCipher, NM_AUTH_TYPE_WPA_PSK_AUTO);
168 break;
170 // now try both ciphers on the raw key
171 if (ieee_802_11_cipher_validate(hexCipher, essid.toUtf8(), rawKey.toUtf8()) == 0)
173 kDebug() << "HEX";
174 cipher = hexCipher;
176 else if (ieee_802_11_cipher_validate(ppCipher, essid.toUtf8(), rawKey.toUtf8()) == 0)
178 kDebug() << "PP";
179 cipher = ppCipher;
181 else
182 *error = true;
184 if (!(*error))
186 // int32 cipher
187 int we_cipher = ieee_802_11_cipher_get_we_cipher(cipher);
188 args << QVariant(we_cipher);
189 // s key
190 char * rawHashedKey = 0;
191 rawHashedKey = ieee_802_11_cipher_hash(cipher, essid.toUtf8(), rawKey.toUtf8());
192 QString hashedKey = QString::fromAscii(rawHashedKey);
193 free(rawHashedKey);
194 args << QVariant(hashedKey);
195 // int32 wpa version
196 if (auth->version() == Solid::Control::AuthenticationWpaPersonal::Wpa1)
197 args << QVariant(IW_AUTH_WPA_VERSION_WPA);
198 else
199 args << QVariant(IW_AUTH_WPA_VERSION_WPA2);
200 // int32 key management
201 if (auth->keyManagement() == Solid::Control::AuthenticationWpaPersonal::WpaPsk)
202 args << QVariant(IW_AUTH_KEY_MGMT_PSK);
203 else
204 args << QVariant(IW_AUTH_KEY_MGMT_802_1X);
205 kDebug(1411) << "Outbound args are: " << args;
207 return args;
210 QList<QVariant> NMDBusHelper::doSerialize(Solid::Control::AuthenticationWpaEnterprise * auth, const QString & essid, QList<QVariant> & args, bool * error)
212 Q_UNUSED(essid)
213 Q_UNUSED(error)
214 kDebug() << "Implement me!";
215 // int32 cipher, always NM_AUTH_TYPE_WPA_EAP
216 args << NM_AUTH_TYPE_WPA_EAP;
217 switch (auth->method())
219 case Solid::Control::AuthenticationWpaEnterprise::EapPeap:
220 args << NM_EAP_METHOD_PEAP;
221 break;
222 case Solid::Control::AuthenticationWpaEnterprise::EapTls:
223 args << NM_EAP_METHOD_TLS;
224 break;
225 case Solid::Control::AuthenticationWpaEnterprise::EapTtls :
226 args << NM_EAP_METHOD_TTLS;
227 break;
228 case Solid::Control::AuthenticationWpaEnterprise::EapMd5:
229 args << NM_EAP_METHOD_MD5;
230 break;
231 case Solid::Control::AuthenticationWpaEnterprise::EapMsChap:
232 args << NM_EAP_METHOD_MSCHAP;
233 break;
234 case Solid::Control::AuthenticationWpaEnterprise::EapOtp:
235 args << NM_EAP_METHOD_OTP;
236 break;
237 case Solid::Control::AuthenticationWpaEnterprise::EapGtc:
238 args << NM_EAP_METHOD_GTC;
239 break;
241 // int32 key type
242 args << NM_AUTH_TYPE_WPA_PSK_AUTO;
243 // s identity
244 args << auth->identity();
245 // s password
246 args << auth->idPasswordKey();
247 // s anon identity
248 args << auth->anonIdentity();
249 // s priv key password
250 args << auth->certPrivatePasswordKey();
251 // s priv key file
252 args << auth->certPrivate();
253 // s client cert file
254 args << auth->certClient();
255 // s ca cert file
256 args << auth->certCA();
257 // int32 wpa version => {IW_AUTH_WPA_VERSION_WPA,IW_AUTH_WPA_VERSION_WPA2}
258 args << auth->version();
259 return QList<QVariant>();